Only show these results:

Send email messages with Python

This page explains how to send email messages with the Nylas Python SDK and Email API.

Before you begin

Before you start, you must have done the following tasks:

Create and send a draft

To create a draft and send it as an email message, first create a Draft object and assign it attributes.

draft = nylas.drafts.create(
grant_id,
request_body={
"to": [{ "name": "Name", "email": email }],
"reply_to": [{ "name": "Name", "email": email }],
"subject": "Your Subject Here",
"body": "Your email body here.",
}
)

The to parameter is an array of email objects that contains names and email addresses. You can use to for cc and bcc.

Next, use drafts.send() to send the draft as an email message.

draft = nylas.drafts.send(
'<NYLAS_GRANT_ID>',
'<DRAFT_ID>',
)

Reply to an email message

The first step to reply to an email message is to find the thread you want to reply to. Then, create a draft that has the same thread_id and subject, as in the following example.

from dotenv import load_dotenv
load_dotenv()

import os
import sys
from nylas import Client

draft = nylas.drafts.create(
'<NYLAS_GRANT_ID>',
request_body={
"to": [{ "name": "Name", "email": email }],
"reply_to": [{ "name": "Name", "email": email }],
"subject": "Your Subject Here",
"body": "Your email body here.",
"thread_id": '<THREAD_ID>'
}
)

draftSent = nylas.drafts.send(
'<NYLAS_GRANT_ID>',
draft[0].id,
)

Attach a file to an email message

The Attachments endpoint allows you to create and modify files that you can attach to email messages. The following example shows how to take a file that's saved locally and upload it to Nylas for use with the Email API.

from nylas.utils.file_utils import attach_file_request_builder

# Attachment to add to draft
attachment = attach_file_request_builder('attachment.pdf')

Next, create a draft to attach the file to.

draft = nylas.drafts.create(
'<NYLAS_GRANT_ID>',
request_body={
"to": [{ "name": "Name", "email": email }],
"reply_to": [{ "name": "Name", "email": email }],
"subject": "Your Subject Here",
"body": "Your email body here.",
"attachments": [attachment]
}
)

draft = nylas.drafts.send(
'<NYLAS_GRANT_ID>',
draft[0].id,
)

Below is the full example showing how to attach a file to an email message.

from dotenv import load_dotenv
load_dotenv()

import os
import sys
from nylas import Client
from nylas.utils.file_utils import attach_file_request_builder

nylas = Client(
os.environ.get('NYLAS_API_KEY'),
os.environ.get('NYLAS_API_URI')
)

attachment = attach_file_request_builder('attachment.pdf')

draft = nylas.drafts.create(
'<NYLAS_GRANT_ID>',
request_body={
"to": [{ "name": "Name", "email": email }],
"reply_to": [{ "name": "Name", "email": email }],
"subject": "Your Subject Here",
"body": "Your email body here.",
"attachments": [attachment]
}
)


draft = nylas.drafts.send(
'<NYLAS_GRANT_ID>',
draft[0].id,
)

Explore the Email API

If you've made it to this point, congratulations! You've learned how to send email messages with the Nylas Node.js SDK and Email API! 🎉

There's plenty more that you can do with Nylas. Take a look at the following resources to learn more.