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:
- Set up your Nylas developer account and get your API keys
- Install the Nylas Python SDK
- Create and Send an Email Draft
- Reply to an email message in a user's inbox
- Attach a file to an email
- Explore the Nylas Email API
Prerequisites
Before you can start using the Nylas Python SDK, make sure you have done the following:
Prerequisites
Before you can start using the Nylas Java SDK, make sure you have done the following:
- Sign up for your developer account.
- Get your developer keys. You need to have your:
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 Java SDK
The Nylas Java SDK requires Java 8 or later.
For the following examples, replace X.X.X
with the version you want to use. See the list of Nylas Java SKD releases to learn about the available versions.
Set up with Gradle
If you're using Gradle, add the following to the dependencies section of your build.gradle
file:
implementation("com.nylas.sdk:nylas-java-sdk:X.X.X")
Setup with Maven
For projects using Maven, add the following to your POM file:
<dependency>
<groupId>com.nylas.sdk</groupId>
<artifactId>nylas-java-sdk</artifactId>
<version>X.X.X</version>
</dependency>
- Ensure you have pip installed on your development environment.
- Create a virtual environment to install Nylas.
Install the Nylas Python SDK
With your virtual environment activated, run:
pip install nylas
Congrats! You’re now ready to write code with the Nylas Python SDK.
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, replaceCLIENT_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 with the os.environ
module.
Create a Message Draft
Now, create a new message 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': '[email protected]'}]
The draft.to
parameter is an array of email objects that contain names and email addresses. This also works for draft.cc
anddraft.bcc
.
Send the Draft
Finally, it’s time to send the email!
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."
draft.to = [{'name': 'My Nylas Friend', 'email': '[email protected]'}]
draft.send()
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()
Next, assign a value to the draft body
, and setup the appropriate values for to
, cc
, and bcc
. Finally, run draft.send()
to send the reply.
draft.body = "This is my reply"
draft.to = thread.from_
draft.cc = thread.cc
draft.bcc = thread.bcc
draft.send()
Here is the entire example to send an email reply.
from nylas import APIClient
nylas = APIClient(
CLIENT_ID,
CLIENT_SECRET,
ACCESS_TOKEN
)
thread = nylas.threads.get('{id}')
draft = thread.create_reply()
draft.body = "This is my reply"
draft.to = thread.from_
draft.cc = thread.cc
draft.bcc = thread.bcc
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': '[email protected]'}]
draft.attach(file)
draft.send()
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
)
attachment = open('attachment.pdf', 'r')
file = nylas.files.create()
file.filename = 'attachment.pdf'
file.stream = attachment
file.save()
attachment.close()
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': '[email protected]'}]
draft.attach(file)
draft.send()
Video Walkthrough
Prefer video? You can watch a short video on getting set up with Nylas and sending your first email using Python.
You can also watch our LiveStream Coding with Nylas.
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.
- Take a look at additional code examples for the Python SDK to see what else you can do with email, calendars, and contacts.
- Learn about some of the more important features of the Nylas Email API.
- Read about how the Nylas Communications Platform works.
- Review our app integration guide to learn about what it takes to integrate the Nylas Email API into your app.
- Read the blog post How to Send Emails with the Nylas Python SDK