This page explains how to use the Nylas Ruby SDK and Email API to send email messages.
Before you begin
Before you start, you must have done the following tasks:
- Installed and set up the Nylas Ruby SDK.
- Authenticated one or more end users.
Create and send an email draft
This section walks through how to create and send an email draft with the Nylas Ruby SDK.
The following example creates a Draft object and assigns it a subject and some body text.
request_body = { subject: 'From Nylas', body: 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.',}
draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
You can also add file attachments, message tracking features, and reply-to values. For more information about the data you can add to a draft, see the Drafts references.
Next, add a recipient to the draft and send it.
Below is the full example showing how to draft and send an email message.
#!/usr/bin/env rubyrequire 'nylas'
nylas = Nylas::Client.new(api_key: "<NYLAS_API_KEY>")
request_body = { subject: 'From Nylas', body: 'This email was sent using the Nylas Email API. Visit https://nylas.com for details.',}
draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)draft, _ = nylas.drafts.send(identifier: "<NYLAS_GRANT_ID>", draft_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. The example below gets the most recent email message in an end user’s inbox by returning only the first thread.
thread, _ = nylas.threads.find(identifier: "<NYLAS_GRANT_ID>", thread_id: "<THREAD_ID>")
Next, create a draft that has the same thread_id
and subject
as the thread you’re replying to, and assign its recipients.
request_body = { subject: thread[:subject], body: 'This is my reply', to: [{ name: thread[:participants][0][:name], email: thread[:participants][0][:email]}], reply_to_message_id: thread[:message_ids][0]}
draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
Below is the full example showing how to reply to an email message.
#!/usr/bin/env rubyrequire 'nylas'
nylas = Nylas::Client.new( api_key: "<NYLAS_API_KEY>")
request_body = { subject: thread[:subject], body: 'This is my reply', to: [{ name: thread[:participants][0][:name], email: thread[:participants][0][:email]}], reply_to_message_id: thread[:message_ids][0]}
draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)draft, _ = nylas.drafts.send(identifier: "<NYLAS_GRANT_ID>", draft_id: "<DRAFT_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 examples show how to take a file that’s saved locally and upload it to Nylas for use with the Email API.
file = Nylas::FileUtils.attach_file_request_builder('attachment.pdf')
Next, create a draft and attach the file to it.
request_body = { subject: "With Love, from Nylas", body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.", attachments: [file]}
nylas.messages.send(identifier: ENV["NYLAS_GRANT_ID"], request_body: request_body)
Below is the full example showing how to attach a file to an email message.
#!/usr/bin/env rubyrequire 'nylas'
nylas = Nylas::Client.new(api_key: "<NYLAS_API_KEY>")file = Nylas::FileUtils.attach_file_request_builder('attachment.pdf')
request_body = { subject: "With Love, from Nylas", body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.", attachments: [file]}
nylas.messages.send(identifier: ENV["NYLAS_GRANT_ID"], request_body: request_body)