Emails with Sendgrid

Daniel Wagener
2 min readDec 15, 2019

--

These are my notes for Jonas Schmedtmann’s Node.js Bootcamp on Udemy: https://www.udemy.com/course/nodejs-express-mongodb-bootcamp/

A More Robust Email Architecture

Right now, our email.js looks like this:

It’s very basic at the moment, so now we’re gonna beef it up with a sophisticated Email class. It’s going to look like this:

The newTransport method creates a the same transporter as before, but will use Sendgrid when the website is running in production mode. At the bottom, sendWelcome with call upon the send class to render a Pug template called welcome with a welcoming subject line. Pug allows us to send an HTML-formatted email. In case we don’t want to send an HTML email, we include a text property that uses the html-to-text package.

To actually send this welcome email, we just have to include a couple of new lines in our signup handler:

Integrating SendGrid

To start using Sendgrid, we create an account, go the setup guide, and integrate using SMTP (when working with nodemailer, we can’t use the SendGrid API). We’ll generate an API key and save the username and password in our config.env. Thankfully, SendGrid is one of the predefined services in nodemailer, so we don’t have to specify the host or the port.

To test this new transporter, we can create a disposable email addrress at mailsac.com, run our app in production mode, and then sign up!

--

--