How to Send Emails in Node.js using Nodemailer
Tags: node, webdev, javascript, programming
Published: 2023-01-18 15:25:42

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uh2hzfjvcgadhb3ucct.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az99797wezbn7z0sgyc4.png)

* install the Nodemailer package by running the following command on vs code terminal, and include the module in your code.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tv802gm0vsy4eete1ek7.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdoqfcjjz7o61xbwe10d.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adgaaarnmh0mt0taciro.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wy8t2lodc4xepunrspfi.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ov3t1vd9waqeem72awul.png)


* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbufslg3yu4cyc7ucy11.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96fw1igan6sbnhvzrtta.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vkhfw8t5nzmxr1w1xs9.png)

###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](https://www.w3schools.com/nodejs/nodejs_email.asp)