Send email with the Nylas Ruby SDK
The Nylas Ruby SDK is the quickest way to integrate email account data into your app with Ruby and the Nylas Email API. The Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and the Ruby SDK makes it easy to read data directly from user email accounts.
This page discusses how to send email with the Nylas Ruby SDK and Email API.
What you'll learn
In this tutorial, you'll learn how to do the following tasks:
- Create a Draft and send it as an email message.
- Reply to an email message.
- Attach files to an email message.
Prerequisites
Before you start this tutorial, you must install the Nylas Ruby SDK and set up your environment. See Get started with the Nylas Ruby SDK for more information.
Step 1: Initialize the API
object
All of the functionality of the Nylas platform is available through the API
object. Before you make requests, initialize the API
object with your client ID, client secret, and access token:
#!/usr/bin/env ruby
CLIENT_ID = 'your_nylas_client_id'
CLIENT_SECRET = 'your_nylas_client_secret'
ACCESS_TOKEN = 'your_account_access_token'
require 'nylas'
nylas = Nylas::API.new(
app_id: CLIENT_ID,
app_secret: CLIENT_SECRET,
access_token: ACCESS_TOKEN
)
For more information on initializing the API
object, see Get started with the Nylas Ruby SDK.
Step 2: (Optional) Change the base API URL
You can choose to change the base API URL depending on your location:
Location | API URL | Scheduler API URL |
---|---|---|
United States (Oregon) | https://api.nylas.com |
https://api.schedule.nylas.com |
Europe (Ireland) | https://ireland.api.nylas.com |
https://ireland.api.schedule.nylas.com |
For more information, see Data residency and Migration guide for data centers.
Pass the API_URL
parameter to change the base API URL:
#!/usr/bin/env ruby
CLIENT_ID = 'your_nylas_client_id'
CLIENT_SECRET = 'your_nylas_client_secret'
ACCESS_TOKEN = 'your_account_access_token'
API_URL='your_base_api_url'
require 'nylas'
nylas = Nylas::API.new(
app_id: CLIENT_ID,
app_secret: CLIENT_SECRET,
access_token: ACCESS_TOKEN,
api_server: API_URL
)
If you're using the Nylas EU region: The base API URL defaults to the US region, but you can specify api_server: 'https://ireland.api.nylas.com'
to use the EU region. See Data Residency for more information.
Step 3: Send an email message
You can create an email draft using Nylas, and send that draft as an email message. You can also reply to email messages and attach files.
Create and send a Draft
Follow these steps to create a Draft and send it:
-
Create a
draft
object and assign thesubject
,body
, and the recipient details (to
):draft = nylas.drafts.create(subject: "With Love, 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]'}])- The
to
parameter is an array of email objects that contains names and email addresses. You can useto
forcc
andbcc
.
- The
-
Use
draft.send!
to send the Draft as an email message.
Learn More
You can also add file attachments, message tracking features, and reply-to values to a draft email message. See the Drafts documentation for more information.
Example: Create and send a Draft
#!/usr/bin/env ruby
CLIENT_ID = 'your_nylas_client_id'
CLIENT_SECRET = 'your_nylas_client_secret'
ACCESS_TOKEN = 'your_account_access_token'
require 'nylas'
nylas = Nylas::API.new(
app_id: CLIENT_ID,
app_secret: CLIENT_SECRET,
access_token: ACCESS_TOKEN
)
draft = nylas.drafts.create(subject: "With Love, 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.send!
Reply to an email message
You can also reply to an email message using the Nylas Ruby SDK:
-
Get the ID of the thread that you want to reply to:
thread = nylas.threads.find('{id}')
-
Use
nylas.drafts.create()
to create a Draft, and set thereply_to_message_id
parameter to the thread ID:draft = nylas.drafts.create(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]) -
Use
draft.send!
to send the Draft as an email message.
Example: Reply to an email message
#!/usr/bin/env ruby
CLIENT_ID = 'your_nylas_client_id'
CLIENT_SECRET = 'your_nylas_client_secret'
ACCESS_TOKEN = 'your_account_access_token'
require 'nylas'
nylas = Nylas::API.new(
app_id: CLIENT_ID,
app_secret: CLIENT_SECRET,
access_token: ACCESS_TOKEN
)
thread = nylas.threads.find('{id}')
draft = nylas.drafts.create(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.sent!
Attach a file to an email message
The Files endpoint enables you to attach files to email messages.
These steps demonstrate how to open a sample PDF file (attachment.pdf
) and attach it to an email message:
-
Upload a new file and retrieve its ID:
file = nylas.files.create(file: File.open(File.expand_path('attachment.pdf'), 'r')).to_h
-
Create an email object and set the
file_ids
parameter to attach the file:nylas.send!(
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.',
file_ids: [file[:id]]
).to_h
Example: Attach a file to an email message
#!/usr/bin/env ruby
CLIENT_ID = 'your_nylas_client_id'
CLIENT_SECRET = 'your_nylas_client_secret'
ACCESS_TOKEN = 'your_account_access_token'
require 'nylas'
nylas = Nylas::API.new(
app_id: CLIENT_ID,
app_secret: CLIENT_SECRET,
access_token: ACCESS_TOKEN
)
file = nylas.files.create(file: File.open(File.expand_path('attachment.pdf'), 'r')).to_h
nylas.send!(
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.',
file_ids: [file[:id]]
).to_h
More resources
If you’ve made it this far, congrats! You now know how to create drafts and send email with the Nylas Email API! 🎉
There's plenty more that you can do with Nylas. Take a look at these resources:
- Follow our Ruby SDK tutorials.
- Learn more about the Nylas API.
- Learn more about the Nylas Email API.
- Read about how the Nylas platform works.
- Read our How to send email with the Nylas Ruby SDK blog post.