Send Emails for your Node.js Application using Nodemailer + Express
Nội Dung Chính
Send Emails for your Node.js Application using Nodemailer + Express
Image by Muhammad Ribkhan from Pixabay
nodemailer
is a simple module that has zero dependencies, designed specifically for sending emails. Follow along with this tutorial of a simple business scenario: a website sends a thank you email to new subscribers.
Step 1: Gmail setup
Nodemailer supports sending emails using Gmail. We do this by registering a Gmail account that uses two-factor authentication. Log in to your business’s Gmail account and open the Account > Security page.
Scroll down until you see ‘Signing in to Google’, then click on ‘2-Step Verification’ as shown below.
Follow the steps on the screen, and enable two-step verification.
When you return to the Account > Security page the 2-step verification should be displayed as shown below. You should see a new section called ‘App passwords’. Click it.
We need to set up a dedicated app password for our application for nodemailer
to work.
Choose mail
for the first parameter, and your <Insert OS>
for the second parameter (my case: Mac), and select ‘Generate’ to generate a password.
Save the 16-digit password inside the yellow highlight for later use.
Step 2: Nodemailer Setup
Install nodemailer
using npm install. Also, install express
as well if you have not done so.
Set up a config object and fill out the keys as so.
- For
host
, use Simple Mail Transfer Protocol (SMTP) as the main transport for delivering messages. - The
port
is the port to connect to, usually set at 587. - The
secure
option istrue
if the connection will use TLS when connecting to the server. Set this tofalse
. - The
auth
is the authentication object, that will containuser
, our sender email, andpass
, the 16-digit password we set earlier.
Create a function that sends the email, with the parameter data
that defines the email content.
The createTransport
function creates a transporter object that sends the email. The transporter contains a module named sendMail
that sends the email given data
and a callback function that runs once the email is sent or had failed to send.
data
contains the email content, and can be to be configured as such. The from
should be your email and to
your recipient’s email address. Fill in the subject title
, and text
thanking your subscribers for opting in.
You can also use
html
andattachment
to send formatted text and attachments as files respectively.
Find out more about message configurations here.
Step 3: Set up Express
To test our nodemailer
, we will set up a simple Express route. Set up a simple express application, start the server, and listen to local port 3000.
Now create an async POST
request to send the email through the route /api/email
. Our request body (req.body
) should contain the data we set up above.
Step 4: Test nodemailer
We can test our setup through Postman, an API platform for building and using APIs. Click on the Body
tab, select raw > JSON
, and fill out the message configurations as such.
Click send, and the recipient will receive an email like so.