How to Send an Email with the Nylas API in Javascript / Node.js

How to Send an Email with the Nylas API in Javascript / Node.js

Learn how quickly you can send an email using the Nylas API and Node SDK.

Jieun Kim | November 25, 2019

According to GitHub’s State of the Octoverse 2019, JavaScript remains the most-used programming language on GitHub. Considering its status as the world’s largest repository host, this means chances are high you’ve coded a little (or a big!) something in JavaScript this past year and are interested in more. Luckily, the Nylas Email API not only connects to all major providers, including Gmail, Outlook, Office365, Exchange, and Yahoo, we also maintain a Node SDK that makes it easy to send emails directly from user email accounts.

There’s a few things you’ll want to get squared away before the fun can begin: 

Assuming you have Node.js installed on your machine, and have npm or yarn, you’ll want to sign up for your Nylas developer account, and follow our guide to get your API keys and authorize your first email account. Once you’ve done that, you’ll have the three tokens necessary to run the code example below:

  • 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

Great, here we go!  

First, head to the nearest command line and run npm install nylas or  yarn add nylas .

Next, you’ll create a Nylas API Client by requiring in ‘nylas’ and calling the ‘config’ function with your CLIENT_ID and CLIENT_SECRET replacing the placeholder values.

Then call the ‘with’ function and pass in the ACCESS_TOKEN you generated above. This allows Nylas to access data for the specific user account associated with that access token.

const Nylas = require('nylas');

Nylas.config({

    clientId: CLIENT_ID,

    clientSecret: CLIENT_SECRET,

});

 

const nylas = Nylas.with(ACCESS_TOKEN);

 

Once that’s done, all that’s left is building the draft object and sending it. You can see in the example code below that you specify a recipient with the ‘to’ parameter. It takes an array of email objects that contain names and email addresses. To check out what other attributes are available to you on the draft object, check out our docs here… and keep sending those missives!

const draft = nylas.drafts.build({

    subject: 'With Love, from Nylas',

    to: [{ name: 'My Nylas Friend', email: '[email protected]' }],

    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`);

});

 

If you want to walk through the above code step-by-step, take a look at our handy tutorial. If you’re raring to learn about some more powerful features of the Nylas Email API, take a look here. Finally, if you simply haven’t gotten enough Node.js, you’ll definitely want to explore our Node SDK Quickstart guide.

About the Author

jieun kim is a technical writer based in nyc. in her spare time, she enjoys contemplating unusual shapes and accumulating large stacks of books.

Ready to Start Building?

Connect up to 10 accounts for free today. No credit card required.