How to Send Emails in JavaScript/Node.js with SendGrid

May 07, 2019
Written by
Sam Agnew
Twilion

Send Emails with Node

So you're building a Node.js app and you need to programmatically send some emails. The Twilio SendGrid API for sending email is a great solution to this problem. If you have a SendGrid account and an API key set as an environment variable, here is all the code you need to send an email in JavaScript:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: test@example.com',
  from: 'test@example.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

Let's walk through how to get this running step by step.

Development Environment Setup

Make sure we have the right software installed that we'll need to use for the rest of this post. Throughout this post you will need:

Here is a good guide to follow on setting up your development environment if you are going to be doing more web development with Node.js.

Sign up for SendGrid and create an API key

The first thing you need to do is create a SendGrid account. You can choose the free tier for the purpose of this tutorial. Once you have an account, you need to create an API key as seen in this screenshot. You can name it whatever you want, but once it is created make sure you save it before moving on!

Creating a SendGrid API Key

A good way to save this API key is to set it as an environment variable that you can access from your JavaScript code in order to avoid writing it directly in your code. Set the value of the SENDGRID_API_KEY environment variable to be the API key from your SendGrid account. Here's a useful tutorial if you need help setting environment variables. We will use this later on.

Sending an Email with Node.js

Now that you have a SendGrid account and an API key, you're ready to dive into some code and send emails! Start by opening your terminal and navigating to the directory where you want your project to live and running the following command to initiate a package.json file for npm to install dependencies:

npm init --yes

Now install the SendGrid helper library for Node:

npm install @sendgrid/mail

Now create a file called index.js in this directory and add the following code to it:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

Before running this code, make sure you have the SENDGRID_API_KEY environment variable set, and remember to replace the to value with your own email address to verify that your code worked correctly.

Finally, in your terminal run the following command to run this code to send yourself a picture of an email:

node index.js

Check your inbox and you should see something like this!

 

The email sent with SendGrid

What next?

You just successfully sent your first email using Node.js, but what if you want to do more than just send emails? You can also process and respond to incoming emails using SendGrid's Inbound Email Parse Webhook. You can also check out the SendGrid docs for a ton of other cool features and uses.

I’m looking forward to seeing what you build. Feel free to reach out and share your experiences or ask any questions.

  • Email: sagnew@twilio.com
  • Twitter: @Sagnewshreds
  • Github: Sagnew
  • Twitch (streaming live code): Sagnewshreds