Only show these results:

Send an Email with Node.js

Beginner

The Nylas Node SDK is the quickest way to integrate email into your app with JavaScript and the Nylas Email API. Build your email integration in 15 minutes.

Node Email Create

Node.js has grown to be the most popular framework in the world, primarily due to it’s efficiency for data-intensive real-time applications and the ability to use JavaScript across the entire web stack. The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and our Node SDK makes it simple to send emails directly from user email accounts.

This guide explains how to use the Nylas Node SDK and Email API to send an email with JavaScript. It covers the following steps:

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Node.js SDK
  3. Create and send an email
  4. Explore the Nylas Email API

Prerequisites

Before you can start using the Nylas Node.js SDK, make sure you have done the following:

  • Sign up for your developer account.
  • Get your developer keys. You need to have your:
    • CLIENT_ID - The CLIENT ID found on the dashboard page for your Nylas App.
    • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App.
  • Get Node.js installed on your machine.
  • Make sure you have npm or yarn.

Install the Nylas Node.js SDK

Run npm install nylas or yarn add nylas.

Congrats! You’re now ready to write code with the Nylas Node.js SDK.

Send an Email

At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email providers. The Nylas object provides access to every resource in the Nylas Email API, so the first thing to do is to initialize it:

const Nylas = require('nylas');   

Then, call the config function and pass the CLIENT_ID and CLIENT_SECRET from earlier in this guide, replacing the placeholders with your values.

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

Take care with your secrets

It’s not best practice to include secrets like this in your code. For increased security, store these values as environment variables.

Next, call the with function and pass it the ACCESS_TOKEN from earlier in this guide. This allows Nylas to access data for a specific user account.

const nylas = Nylas.with(ACCESS_TOKEN);   

Create a Draft

Now, it’s time to build your first draft object:

const draft = nylas.drafts.build({
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: 'swag@nylas.com' }]
});

This code example creates a new draft object and assigns it a subject and body text. You can also add file attachments, message tracking features, and reply-to values. Take a look at the Nylas API reference to learn more about what you can add to an email draft with the Nylas Email API.

The example also assigns a recipient with the to parameter. It’s an array of email objects that contain names and email addresses. This also works for cc and bcc.

Finally, it’s time to send the email!


draft.send().then(message => {
console.log(`${message.id} was sent`);
});

The following example combines all of this code into a single block, for easier reference.


const Nylas = require('nylas');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

const nylas = Nylas.with(ACCESS_TOKEN);

const draft = nylas.drafts.build({
subject: 'With Love, from Nylas',
to: [{ name: 'My Nylas Friend', email: 'swag@nylas.com' }],
body: 'This email was sent using the Nylas email API. Visit https://nylas.com for details.'
});

// Send the draft
draft.send().then(message => {
console.log(`${message.id} was sent`);
});

Video Walkthrough

Prefer video? You can watch a short video on getting set up with Nylas and sending your first email using Node.js.

Explore the Nylas Email API

If you’ve made it to this point, congratulations! You’ve sent your very first email with the Nylas Email API! There is plenty more that you can do with it; take a look at the following resources to learn more about the Nylas Communications Platform capabilities.