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.
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.
install the Nodemailer package by running the following command on vs code terminal, and include the module in your code.
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.
- 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.
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.
The complete code required to send email using nodemailer is as follow.
- 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.
After the code runs, if there is an error, it logs the error message on the console or logs "email sent successfully".
The email is received once the success message is logged on the console.
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
Top comments (1)
Thanks for sharing. As well, a light & powerful package to deal with email sending in node.js, with web api's and / or smtp transporters: github.com/steve-lebleu/cliam