How to use SendGrid with Nodemailer to send mails.

Pankaj Jaiswal
2 min readAug 9, 2020

Step 1: Making an account and verifying a single sender.

I am assuming that you’ve already created an account on SendGrid, I will proceed with how and why we need to verify a single sender.

If you do not have a single sender new version of SendGrid won’t send mail. It will throw an error of Forbidden.

For creating verified single sender go to your SendGrid dashboard>Sender Authentication>Single Sender Verification>Create new Sender.

Note: Form email should be a real email as SendGrid sends verification link. Replay to can be anything

Ref: https://sendgrid.com/docs/ui/sending-email/sender-verification/

Step 2: Integrating SendGrid SMTP with Nodemailer

After creating verified single sender go to Email API>Integration Guide and select SMTP Relay.

API key name can be anything, after clicking on Create Key you will get password copy it and save it somewhere we will need it later.

Now you can open your terminal and install two packages

npm i nodemailer nodemailer-sendgrid

You can store your Generated Key in .env or use directly in code as per your preference.

Create Nodemailer transport

const nodemailer = require('nodemailer');const nodemailerSendgrid = require('nodemailer-sendgrid');const transport = nodemailer.createTransport(nodemailerSendgrid({     apiKey: process.env.SENDGRID_API_KEY  }));

Send a message

transport.sendMail({   from: 'xyz@gmail.com',   to: 'Receiver Name <receiver@example.com> ,        someother@example.com',  subject: 'hello world',  html: '<h1>Hello world!</h1>'});

Note: From mail should same mail that you’ve verified, If you put any other mail it will throw errors.

Thanks for reading this post. Sorry for the typo if any😁.

--

--