This page explains how to send messages with the Nylas Python SDK and Email API.
Before you begin
Section titled “Before you begin”Before you start, you must have done the following tasks:
- Installed and set up the Nylas Python SDK.
- Authenticated one or more users.
Create and send a draft
Section titled “Create and send a draft”To create a draft and send it as a 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 a message.
draft = nylas.drafts.send( '<NYLAS_GRANT_ID>', '<DRAFT_ID>',)
Reply to a message
Section titled “Reply to a message”The first step to reply to a 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_dotenvload_dotenv()
import osimport sysfrom 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 a message
Section titled “Attach a file to a message”The Attachments endpoint allows you to create and modify files that you can attach to 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 draftattachment = 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 a message.
from dotenv import load_dotenvload_dotenv()
import osimport sysfrom nylas import Clientfrom 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,)