How to Send an Email with the Python SDK

How to Send Email with the Python SDK

4 min read
Tags:

Paul Graham recently wondered whether there was a start-up that would email you once a day asking ‘What’s happening?’ and accumulate the responses. This inquiry launched a flurry of brainstorming wide-ranging ideas for productivity tools, but what do these all have in common? Sending email is the fundamental building block. Luckily, the Nylas API abstracts away the complications of sending email, so you can concentrate on the fun parts of your application! To that end, let’s go ahead and take a look at how you can use Nylas’ Python SDK to send email.

Setting-up Nylas’ Python SDK

First, make sure you have pip installed on your development environment and create a virtual environment to install Nylas. If you’d like, refer to detailed directions outlined in the Python docs. Then, with your virtual environment activated, run pip install nylas.

Now, sign up for a developer account and follow our guide to getting your client_id, client_secret, and access_token –  these three tokens are all you need to jump right in.

Send an Email With the Python SDK

Because the Nylas Communication Platform is an API client that interfaces with all of the major email providers, we’ll always want to configure the API Client first. To do so, import the APIClient class from the nylas package and create a new instance of this class and pass in the three pieces of information you obtained from the set-up steps above:

from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN,
)

When using the SDK to send email in Python, a draft must first be created. So that’s what we’ll do next:

draft = nylas.drafts.create()
draft.subject = "What’s happening?"
draft.body = "Reply with any notes or agenda you’ve got planned for today!"

What we’ve done here is to create a new draft object and assign it pertinent attributes. For example, a subject and a body are a good start! If you’d like to know what other attributes you can utilize, such as file attachments, reply-to values and more, check out our Nylas API reference.

Next, we definitely want to add the to field. To, cc, and bcc fields are all set with an array of email objects and each email object contains an email value and optional name.

draft.to = [{'name': 'Paul Graham', 'email': 'paul.graham@paulgraham.com'}]

All that’s left now is to send the email:

draft.send()

What if you wanted to add functionality to reply to an email?

Reply to an Email With the Python SDK

Glad you asked! The first step is to get the ID for the corresponding thread that is being replied to – this ID will be used when creating the reply draft object.

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

Next, we’ll want to execute .create_reply() on the thread which creates a draft object with its message_id set to the id for the thread. This ensures that your app will nicely nest the messages within the same thread as your users will expect—

draft = thread.create_reply()

Like we saw earlier, body is an attribute available in a draft object, so we can add that here:

draft.body = "Today, I’ve got four consecutive meetings (Ringo, Paul, John, George) and a presentation taking place tomorrow I need to prepare for"

Can’t forget to send!

draft.send()

And there you have it, your app has logged an entry into the ‘What’s happening?’ annals of 2020.  If you’d like to see what other things you can do in your application, check out our tutorial here. If you’re feeling further inspired and would like to see what else you can create using the Nylas API, take a look at our guides and tutorials below.

Explore the Nylas API

Get Started – Build your first integration with Nylas in 15 minutes.

Quickstart Guides – Get up to speed quickly with our SDKs for Python, Node.js, and Ruby or explore the Nylas Email, Calendar, and Contacts APIs.

How Nylas Works – Take a look at the Nylas architecture to see how we sync billions of emails.

Tutorials – Check out our tutorials to learn how to carry out common functionality like creating, reading and RSVP-ing to calendar events.

Integration Guides – Our integration guides cover what it takes to incorporate email functionality into your app. They cover best practices for using the Nylas Communications Platform and provider-specific advice for Google, Microsoft, IMAP, and more.

Set up Postman – Postman makes it easy to explore the Nylas Email API.

Related resources

How to create and read Google Webhooks using Ruby

Create and read your Google webhooks using Ruby and Sinatra, and publish them using Koyeb. Here’s the full guide.

Build mail merge and email templates using TinyMCE Rich Text Editor

Learn how to build Mail Merge and Email Template functionality in your email workflow using TinyMCE Rich Text Editor.

Send emails using TinyMCE Rich Text Editor

Learn how to improve your email workflow using TinyMCE Rich Text Editor.