How to Send Emails in Node.js using Nodemailer

scofieldidehen

Scofield Idehen

Posted on January 18, 2023

How to Send Emails in Node.js using Nodemailer

Emails are used to communicate with users of a web application in a timely and convenient way. You must use a third-party service or set up a personal mail server to send emails in a web application.

To set up a personal mail server, you must configure the server with the appropriate credentials and set it up with the necessary protocols like SMTP; then, you use an email library or API to build the email message and send it to the intended recipients.

Nodemailer is the most common email library to use as a nodeJS developer; This article covers how to send emails using the Nodemailer library.

Prerequisites

The reader should be familiar with HTML, CSS, javascript, and Node.js and have good familiarity with the vs code interface to fully grasp the content of this article.

Steps to send an email using Nodemailer

  • create a js file, and name it 'app.js'. 
  • run npm init on your vscode terminal.

Image description

This creates a package.json file for your project. The package.json file is a file used to manage a project's dependencies and other information relevant to a project. 
 
Dependencies are external libraries or modules that a project depends on to function correctly.  
 

  • create an HTTP server by writing out the following code.

Image description

  • install the Nodemailer package by running the following command on vs code terminal, and include the module in your code.
     
    Image description

  • Create a transporter for your mail; here, you include your email address and password. 

Note that the password is not your Gmail password; you follow these steps. 

Click on your google account icon by the top right corner of your browser, select 'manage your google account'.

Go to 'security', enable 2-step verification, then set the application password, and select 'others' from the dropdown; it generates a 16 digit password.

Use that as your Gmail password when writing this code.

Image description

  • Create mail details for your application; this contains the details of the sender and also the email address of the receiver and the content of the message you want to send. 

Image description

You can send an email to more than one receiver, by adding them to the “to” property in mailDetails property and separating them by commas.

  • Send the mail using the following code.

Image description

The complete code required to send email using nodemailer is as follow.

Image description

  • Run the code on your terminal by running "node app.js". 

Note that app.js is the name of the js file I created in step 1; you should replace app.js with the name of your js file.

Image description

After the code runs, if there is an error, it logs the error message on the console or logs "email sent successfully".

Image description

The email is received once the success message is logged on the console.

Image description

Conclusion 

Using Nodemailer library to send emails in Node.js is a straightforward process that can be accomplished in just a few lines of code. 

You can easily send an email efficiently and effectively using your email account and customize the message to fit your needs by following the steps outlined in this article. 

Also, I will advise the reader to complete the sub-step in step 5 to ensure that
 emails are delivered safely using Nodemailer.
 
 ###Resources
w3school

💖 💪 🙅 🚩
scofieldidehen
Scofield Idehen

Posted on January 18, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related