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.
Steps to send an email using nodemailer
- The first step is to create a js file, and name it ‘app.js’.
- run npm init on your vscode terminal,
- 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, 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.
- 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.