Nylas Docs

The Nylas Developer Hub

Welcome to the Nylas developer hub. You'll find comprehensive guides and documentation to help you start working with Nylas as quickly as possible, as well as support if you get stuck. Let's jump right in!

Developer Guide

Send an Email with Python

Learn how to send emails from Gmail, Outlook, and Exchange with the Nylas Python SDK. Build your email integration in 15 minutes.

Python is one of the most popular programming languages in the world due to its extensive collection of libraries, easy-to-understand syntax, and practical abstraction capabilities. We love Python so much that we’ve used it to build the Nylas Communications Platform, which enables developers to quickly integrate email, calendar, and contacts into their app. The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and our Python SDK makes it simple to read emails directly from user email accounts.

This guide explains how to use the Nylas Python SDK and Email API to read emails. It covers the following steps:

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Python SDK
  3. Create and Send an Email Draft
  4. Reply to an email message in a user's inbox
  5. Attach a file to an email
  6. Explore the Nylas Email API

Create Your Free Developer Account

Ready to build your email integration? Create your Nylas developer account to get started.

Set up Your Developer Account

After signing up for your Nylas developer account, follow our guide to get your API keys and authorize your first email account. When you complete this guide, you will have three tokens that you need to run the code examples below:

  • CLIENT_ID - The CLIENT ID found on the dashboard page for your Nylas App
  • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App
  • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App

Install the Nylas Python SDK

Prerequisites

  • Ensure you have pip installed on your development environment.
  • Create a virtual environment to install Nylas.

Refer to the official Python documentation on pip and virtualenv to learn more about how to set this up.

Installing the SDK

With your virtual environment activated, run pip install nylas.

Congrats! You’re now ready to write code with the Nylas Python SDK.

Send an Email

Configure the API Client

At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email providers. First, import the APIClient class from the nylas package, and create a new instance of this class, passing the variables you gathered when you got your developer API keys. In the following example, replace CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN with your values.

from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN,
)

Take care with your secrets

It’s not best practice to include secrets like this in your code. A more secure way to provide these values is to store them as environment variables and access them with the os.environ module.

Create a draft

Now, create a new draft, and add some content to it:

draft = nylas.drafts.create()
draft.subject = "With Love, from Nylas"
draft.body = "This email was sent using the Nylas Email API. Visit https://nylas.com for details."

This code example creates a new draft object and assigns it a subject and body text. You can also add file attachments, message tracking features, and reply-to values. Take a look at the Nylas API reference to learn more about what you can add to an email draft with the Nylas Email API.

Next, let’s add a recipient to this email.

draft.to = [{'name': 'My Nylas Friend', 'email': 'swag@nylas.com'}]

The draft.to parameter is an array of email objects that contain names and email addresses. This also works for draft.cc and draft.bcc.

Send the draft

Finally, it’s time to send the email!

draft.send()

Here are all of the code examples combined into a single block for easy reference:


from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN,
)

draft = nylas.drafts.create()
draft.subject = "With Love, from Nylas"
draft.body = "This email was sent using the Nylas Email API. Visit https://nylas.com for details."
# to, bcc, and cc are set with an array of email objects
# Email objects contain an email value and optional name
draft.to = [{'name': 'My Nylas Friend', 'email': 'swag@nylas.com'}]

draft.send()
Ready to Build Your Email Integration?

Reply to an Email

The first step to creating an email reply is to get the id for the corresponding thread that you want to reply to. Then, you can execute .create_reply() on the thread which creates a draft object with it's message_id set to the id for the thread.

thread = nylas.threads.get('{id}')
draft = thread.create_reply()

To send the reply, assign a value to the draft body and run .send().

draft.body = "This is my reply"
draft.send()

Here is the entire example to send an email reply.

from nylas import APIClient
nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN
)
# Send a reply to a thread
thread = nylas.threads.get('{id}')
draft = thread.create_reply()
draft.body = "This is my reply"
draft.send()

Attach a File to An Email

The Files endpoint allows you to create and modify files that can be attached to emails. This example will open a pdf file named attachment.pdf and save it as a file object on Nylas.

attachment = open('attachment.pdf', 'r')
file = nylas.files.create()
file.filename = 'attachment.pdf'
file.stream = attachment
file.save()
attachment.close()

Once the file is saved to Nylas, you can call .attach() on a draft object and pass the file to attach it to the email draft. The following example creates a new draft to attach the file to, but you can also find an existing draft by using nylas.drafts.get('{id}'), replacing {id} with the appropriate draft id.

draft = nylas.drafts.create()
draft.subject = "With Love, From Nylas"
draft.body = 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.'
draft.to = [{'name': 'My Nylas Friend', 'email': 'swag@nylas.com'}]
draft.attach(file)
draft.send()
Ready to Build Your Email Integration?

Here is the full example for creating a file and attaching it to a new email draft.

from nylas import APIClient
nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN
)

# Create a new file with the stream interface for binary objects
attachment = open('attachment.pdf', 'r')
file = nylas.files.create()
file.filename = 'attachment.pdf'
file.stream = attachment
# .save() saves the file to Nylas, file.id can then be used to attach the file to an email
file.save()
attachment.close()

# Create a new email draft to attach the new file to.
draft = nylas.drafts.create()
draft.subject = "With Love, From Nylas"
draft.body = 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.'
draft.to = [{'name': 'My Nylas Friend', 'email': 'swag@nylas.com'}]
draft.attach(file)
draft.send()

Explore the Nylas Email API

If you’ve made it to this point, congratulations! You’ve sent your very first email with the Nylas Email API! There is plenty more that you can do with it; take a look at the following resources to learn more about the Nylas Communications Platform capabilities.

Send an Email with Python


Learn how to send emails from Gmail, Outlook, and Exchange with the Nylas Python SDK. Build your email integration in 15 minutes.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.