Only show these results:

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

  1. Create a draft object and assign the subject, 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 use to for cc and bcc.
  • You can also add file attachments, message tracking features, and reply-to values. Learn more about Drafts.
  1. 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

  1. Get the ID of the thread that you want to reply to.

    thread = nylas.threads.find('{id}')   
  2. Use nylas.drafts.create() to create a draft and set reply_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])
  3. 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.

  1. 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   
  2. 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: