Send email with the Nylas Ruby SDK
The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and the Nylas Ruby SDK makes it easy to read and send emails directly from user email accounts.
This guide explains how to use the Nylas Ruby SDK and Email API to send email.
What you'll learn
In this tutorial, you'll learn how to do the following:
- Create a draft and send the draft as an email message.
- Reply to an email message.
- Attach files to email messages.
Prerequisites
Prerequisites include installing the Nylas Ruby SDK and setting up your environment. Before you start this tutorial, make sure that you complete the Getting started with the Nylas Ruby SDK tutorial.
Step 1: Initialize the API object
All of the functionality of the Nylas Communications 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, refer to the Getting started with the Nylas Ruby SDK tutorial.
Step 2: Change the base API URL
You can optionally 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 API_URL
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
)
Step 3: Send email
Learn how to create a draft and send the draft as an email message. You can also reply to an email message or attach files.
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 contain names and email addresses. You can useto
forcc
andbcc
. - You can also add file attachments, message tracking features, and reply-to values. Learn more about Drafts.
- Use
draft.send
to send the draft.
draft.send!
Example: Create a draft and send an email
#!/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
-
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 setreply_to_message_id
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]) -
Send the draft.
draft.sent!
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 files
The Files endpoint enables you to attach files to email messages.
-
Upload a new file and retrieve the file ID.
The following example opens a PDF file (
attachment.pdf
) and saves it as a file object on Nylas.file = nylas.files.create(file: File.open(File.expand_path('attachment.pdf'), 'r')).to_h
-
Create an email object and set
file_ids
to attach the file to the email object.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 files
#!/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
Explore the Nylas Email API
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 the following resources:
- Follow our Ruby SDK tutorials.
- Learn more about the Nylas API.
- Learn more about the Nylas Email API.
- Read about how the Nylas Communications Platform works.
- Read the blog post How to send email with the Nylas Ruby SDK.