How to Send Emails with the Nylas Ruby SDK

Learn how to use the Nylas Ruby SDK to send emails and attachments via APIs

Ruby

With traditional emailing services, it’s often unknown whether a message will land in the recipient’s inbox, as smart filters can send the message straight into the spam folder. Using the Nylas Connectivity APIs we can ensure that our message will avoid the spam folder with 99.6% probability because the message will be sent from the sender’s individual email account. Additionally, we get the advantage of controlling everything at the API level.

To make things even easier for Ruby developers, we offer the Nylas Ruby SDK. Today we’re going to review how to send emails and attachments with Ruby using the Nylas SDK.

Installing Ruby

Chances are that we already have Ruby installed on our machines. We can check our version by doing the following. (Note that we support Ruby 2.3 and onwards.)

$ ruby -v

ruby 2.6.8p205 (2021–07-07 revision 67951) [Universal.arm64-darwin21]

If we don’t happen to have Ruby installed, we can use any of these methods depending on our operating system.

A note about Ruby on Macs

If we’re on Mac, Ruby 2.x will be installed by default, but this version is tied to the operating system and we might get some errors while trying to install gems. We should leave this particular install of Ruby alone.

Instead, we will install rbenv, which is a Ruby Installation Management System. If we don’t have Homebrew already installed, we can just copy and paste this command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then type these commands on the terminal. The first will install rbenv and the second will simply add to the path:

$ brew install rbenv ruby-build
$ export PATH="$HOME/.rbenv/shims:$PATH"

Once we have rbenv installed, we can choose the version of Ruby that we want to install, by typing these commands on the terminal. The first one will initialize rbenv and the second one will show us the available installations:

$ rbenv init
$ rbenv install -l
List of available Ruby installations

We’re going to choose 3.1.1, so need to type the following commands. The first will install Ruby 3.1.1, the second will define this version as the default, and the last one will give the version currently being used.

$ rbenv install 3.1.1
$ rbenv global 3.1.1
$ ruby -v

ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [arm64-darwin21]

This will install the latest Ruby version for us.

Installing the Nylas Ruby SDK

We can get the Nylas Ruby SDK package from the gem package manager like this:

$ gem install nylas

Apple stopped bundling OpenSSL with MacOS 10.11. However, one of this gem’s dependencies, EventMachine, requires it. If we’re on El Capitan and above and are unable to install the gem, we can try running the following commands in a terminal:

First, we need to check if we have openssl installed with the following command

$ which openssl

/opt/homebrew/bin/openssl

If we don’t have it, we can install it and then instead of manually building the links we can use the second command to have brew make those for us

$ brew install openssl
$ brew link openssl --force

Now, we can proceed with installing the Nylas gem

$ gem install nylas

Now, if we’re getting an error about an invalid certificate, we can simply install another useful gem called certified. This gem ensures that we use a certificate and will provide one in case we don’t have one.

$ gem install certified

Storing our Nylas tokens

Let’s go to the Nylas Dashboard (creating a new account if necessary) and get our CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN.

Nylas Dashboard - App Settings

If we go to App Settings on the left-side menu, we will find a “Developer Details” section. The CLIENT_ID and CLIENT_SECRET, our Nylas account credentials, are there.

For the connected user account’s ACCESS_TOKEN, we can go to Accounts in the left-side menu, select our account, and then press “Generate New Token”. This will generate a new token for the connected account, allowing us to take actions on the user’s behalf via API.

We now need to store all of our tokens in a safe way in our local project. For that, we’re going to install a package called ruby-dotenv which will help us store environment variables that can be accessed by our code:

$ gem install dotenv

Once installed, and in the root folder where our scripts are going to live, we need to create a file called .env:

$ nano .env

Once the file is created, we need to type the following

export CLIENT_ID = <CLIENT_ID>
export CLIENT_SECRET = <CLIENT_SECRET>
export ACCESS_TOKEN = <CLIENT_TOKEN>

Then CTRL+O to save and CTRL+X to exit.

Note that you should never commit your `.env` file or tokens to source control like git or services like GitHub.

Sending an Email using Ruby

Now we get to write some code! For my code editor, I’m going to use Geany; however, you can use the IDE of your choice.

I called this file SendEmail.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV['CLIENT_ID'],
    app_secret: ENV['CLIENT_SECRET'],
    access_token: ENV['ACCESS_TOKEN']
)

# Send your email!
nylas.send!(
    to: [{ email: 'alvaro.t@nylas.com', name: 'Blag' }],
    subject: 'With Love, from Nylas',
    body: 'This email was sent using the Ruby SDK for the Nylas Email API. Visit https://www.nylas.com for details.'
    ).to_h

We can run this script from the terminal by using:

$ ruby SendEmail.rb

If everything worked fine, then we can simply open the recipient’s email inbox and read the email we just sent from the Nylas SDK:

Email sent from Nylas Ruby SDK

Sending an Email with Attachments using Ruby

Just like we can easily send an email, we can add attachments simply as well.

See the new section in the code below under the Create your attachment comment. Note that, in this example, you’ll need an image named “RubyLogo.png” stored in the same folder as your code. (If you want to use the Ruby logo, you can get it here.)

I called this file SendEmailAttachment.py:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV['CLIENT_ID'],
    app_secret: ENV['CLIENT_SECRET'],
    access_token: ENV['ACCESS_TOKEN']
)

# Open a file and create an attachment
file = nylas.files.create(file: File.open(File.expand_path('RubyLogo.png'), 'r')).to_h

# Send your email with attachments!
nylas.send!(
    to: [{ email: 'alvaro.t@nylas.com', name: 'Blag' }],
    subject: 'Ruby with Attachment',
    body: 'This email was sent using the Ruby SDK for the Nylas Email API. Visit https://www.nylas.com for details',
    file_ids: [file[:id]]
    ).to_h

We can run this script from the terminal by using:

$ ruby SendEmailAttachment.rb

If everything worked fine, then we can simply open the recipient’s email inbox and read the received email:

Email with attachment sent from Nylas Ruby SDK

And that’s it! As you can see, by using the Nylas Ruby SDK, sending emails becomes an easy task. If you want to learn more, visit our Documentation Page.

Don’t miss the action, watch our LiveStream Coding with Nylas:

Tags:

You May Also Like

How to create and read Webhooks with PHP, Koyeb and Bruno
How to create a scheduler with Python and Taipy
How to create and read Google Webhooks using Kotlin

Subscribe for our updates

Please enter your email address and receive the latest updates.