Skip to content
Skip to main content

Send messages with Ruby

Last updated:

This page explains how to use the Nylas Ruby SDK and Email API to send messages.

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

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.',
to: [{ name: 'My Nylas Friend', email: '[email protected]'}]
}
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 a message.

#!/usr/bin/env ruby
require '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.',
to: [{ name: 'My Nylas Friend', email: '[email protected]'}]
}
draft, _ = nylas.drafts.create(identifier: "<NYLAS_GRANT_ID>", request_body: request_body)
draft, _ = nylas.drafts.send(identifier: "<NYLAS_GRANT_ID>", draft_id: "<DRAFT_ID>")

The first step to reply to a message is to find the thread you want to reply to. The example below gets the most recent message in a 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 a message.

#!/usr/bin/env ruby
require '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>")

The Attachments endpoint allows you to create and modify files that you can attach to 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 = {
to: [{name: 'My Nylas Friend', email: '[email protected]'}],
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 a message.

#!/usr/bin/env ruby
require 'nylas'
nylas = Nylas::Client.new(api_key: "<NYLAS_API_KEY>")
file = Nylas::FileUtils.attach_file_request_builder('attachment.pdf')
request_body = {
to: [{name: 'My Nylas Friend', email: '[email protected]'}],
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)