Using the Nylas Node SDK to send emails with minimal code.
Ram Bansal | May 11, 2022
Intro
In this post, we are going to learn how to use the Nylas Node SDK to send emails. This will be useful for streamlining sending emails with minimal coding. We’ll first set up our environment, and then we’ll start sending emails using JavaScript. You can also find example code on the Nylas Samples code repository.
Prerequisites
To follow along, you’ll need a Nylas account. Sign up here if you need to create a Nylas account for free!
Want a PDF of this article?
Share it with a friend or save it for later reading.
For more information about how this data is used, please view our Privacy Policy
Once you’ve signed up, follow the Quickstart in the Nylas Dashboard to receive your account credentials. During the Quickstart, you’ll receive an access token; hang on to it so we can use it below.
Environment Setup
Let’s check that our environment is set up to use the Nylas Node SDK. Check the Node version in your terminal:
$ node -v
v18.0.0
If you don’t see a version returned, you may not have Node installed. Try the following steps:
Recommended: If you happen to use or require multiple versions of Node, consider using nvm
The minimum required Node version is v16.0.0. As a quick check, try running node -v again to confirm the version. You may need to restart your terminal for the changes to take effect.
Installing Nylas Node SDK
Let’s start a new project to build from. Skip this step if you are adding Nylas to an existing codebase.
Begin by creating and moving into a new directory, then initializing a new npm project with a default package.json:
$ mkdir node-email-send
$ cd node-email-send
$ npm init –yes
After creating a repository, install the Nylas Node SDK:
$ npm install nylas
Now we have Nylas installed and are ready to add environment variables.
Environment Variables
We are going to install dotenv to manage environment variables. Let’s install dotenv and create a .env file to store environment variables:
$ npm install dotenv
$ touch .env
Next, let’s create the .env file to store environment variables:
We can get the CLIENT_ID and CLIENT_SECRET from the App Settings
Never share the contents of the .env publicly or place the contents on a remote git repository like Github for others to access
For the recipient address (RECIPIENT_ADDRESS), feel free to send it to a fellow colleague, yourself, or use [email protected]
With the environment variables set up, let’s start sending emails.
Sending Emails Using JavaScript
Now for some fun! Let’s create sendEmail.js using the code editor of your choice:
// node-email-send/sendEmail.js
// Import your dependencies
import dotenv from "dotenv/config.js";
import Nylas from "nylas";
import Draft from "nylas/lib/models/draft.js";
// Configure your Nylas client
Nylas.config({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
const nylas = Nylas.with(process.env.ACCESS_TOKEN);
// Create a draft email
const draft = new Draft.default(nylas, {
subject: "With Love, from Nylas",
body: "Hey there, I am sending this email using Nylas, visit https://dashboard.nylas.com/register to try it out!",
to: [{ name: "Recipient name", email: process.env.RECIPIENT_ADDRESS }],
});
// Send the email
try {
const message = await draft.send();
console.log(`Message "${message.subject}" was sent with ID ${message.id}`);
} catch (err) {
console.error("Error:\\n", err);
}
The Nylas Node SDK wraps the Nylas API to draft and send an email. Let’s try sending the email via the terminal:
$ node sendEmail.js
Message "With Love, from Nylas" was sent with ID MESSAGE_ID
If everything worked, you will see the message in the terminal where MESSAGE_ID will be unique for each message sent. This is an example of what the email will look like once received:
Now we’ve sent one of our first emails using the Nylas Node SDK.
Sending Emails with Attachments using JavaScript
Let’s try sending emails with attachments using the Nylas Node SDK. We’ll add the Node.js logo as an attachment. Save the logo in the node-email-send folder, or your project folder, as Node-logo.png.
Let’s create sendEmailAttachment.js:
// node-email-send/sendEmailAttachment.js
// Import your dependencies
import dotenv from "dotenv/config.js";
import Nylas from "nylas";
import Draft from "nylas/lib/models/draft.js";
import File from "nylas/lib/models/file.js";
import fs from "fs";
// Configure your Nylas client
Nylas.config({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
const nylas = Nylas.with(process.env.ACCESS_TOKEN);
// Update a file to attach
const fileData = await fs.readFileSync('./Node-logo.png');
const file = new File.default(nylas, {
data: fileData,
contentType: 'image/png',
filename: 'Node-logo.png'
})
const uploadedFile = await file.upload();
// Create a draft email
const draft = new Draft.default(nylas, {
subject: "With Love, from Nylas",
body: "Hey there, I am sending this email using Nylas, visit https://dashboard.nylas.com/register to try it out!",
to: [{ name: "Recipient name", email: process.env.RECIPIENT_ADDRESS }],
// attach uploaded files
files: [uploadedFile],
});
// Send the email
try {
const message = await draft.send();
console.log(`Message "${message.subject}" was sent with ID ${message.id}`);
} catch (err) {
console.error("Error:\\n", err);
}
Let’s try running this script in the terminal:
$ node sendEmailAttachment.js
Message "With Love, from Nylas" was sent with ID MESSAGE_ID
If everything worked, you will see the message in the terminal where MESSAGE_ID will be unique to each message. This is an example of what the email will look like once received:
Now we’ve sent another email with an attachment using the Nylas Node SDK.
Build time!
You can find example code on the Nylas Samples code repository. Continue building with Nylas and learn more by visiting the Node SDK documentation.
Ram Bansal
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible 🚀. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato 🍅.
Privacy Overview
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!