- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
This blog post contains code for the Nylas API V2. Take a look at your latest docs to see Nylas API V3 Email Docs updated to work with Nylas API V3.
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.
To follow along, you’ll need a Nylas account. Sign up here if you need to create a Nylas account for free!
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.
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:
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.
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.
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:
# node-email-send/.env ACCESS_TOKEN = "ACCESS_TOKEN" CLIENT_ID = "CLIENT_ID" CLIENT_SECRET = "CLIENT_SECRET" RECIPIENT_ADDRESS = "RECIPIENT_ADDRESS"
CLIENT_ID
and CLIENT_SECRET
from the App Settings.env
publicly or place the contents on a remote git repository like Github for others to accessRECIPIENT_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.
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-v3.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.
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-v3.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.
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 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 ????.