Sending Email Using Node.JS

For sending an email using Node.js, we need a node package called nodemailer.

Before this, we need to set up the Node environment. To download the latest version of Node, click this link and download.

Once downloaded, install the local environment and make it ready. For confirming the Node installation, open command prompt and type the following command and press enter.

node -v
 
We will get the currently installed version of Node.js. After completion of all this, follow the below steps.
  1. Install the nodemailer package using Node.js command prompt with this command. 
npm install nodemailer –s

Sending Email Using Node.JS
  1. Once installed, the package.json file will be modified with the dependencies of the nodemailer package.
  2. After installing nodemailer package, import the file to our node.js file which we are using for sending an email. For importing any node package, we need to use -
    1. var nodemailer = require('nodemailer');  
  1. Here, we have created an instance for nodemailer package as nodemailer. Now, we need to use the createTransport method for assigning the host and credentials for authentication. Find the below one.
    1. var transporter = nodemailer.createTransport({  
    2.                 host: 'mail.yourserver.com',  
    3.                 auth: {  
    4.                     user: [email protected]',  
    5.                     pass: 'password'  
    6.                   }  
    7.             }) 
  1. Once we're finished with the above things, we need to construct the list of objects which are required to send an email. Check below.
    1. var mailOptions = {  
    2.                             from: emailFrom,  
    3.                             to: emailTo,  
    4.                             cc: emailCc,  
    5.                             bcc: emailBcc,  
    6.                             subject: emailSubject,  
    7.                             html: emailContent  
    8.                           }; 
  1. Now, we need to use the sendMail method with transporter instance as mentioned below. For this method, we need to pass the mailOptions variable which contains the details of sending an email and we have added the callback function.
    1. transporter.sendMail(mailOptions, function(error, info) {  
    2.                 if (error)  
    3.                 {  
    4.                     res.send([{  
    5.                         result: "failed"  
    6.                     }]);  
    7.                 }  
    8.                 else  
    9.                 {  
    10.                     res.send([{  
    11.                         result: "success"  
    12.                     }]);  
    13.                 }  
    14.             });  
  1. Now, the below part will summarize everything.
    1. const express = require('express');  
    2. const app = express();  
    3. var nodemailer = require('nodemailer');  
    4.   
    5. app.get('/sendmail', (req, res) => {  
    6.   var transporter = nodemailer.createTransport({  
    7.     service: 'gmail',  
    8.     auth: {  
    9.       user: '[email protected]',  
    10.       pass: 'gmail_account_password'  
    11.     }  
    12.   });  
    13.   var mailOptions = {  
    14.     from: 'DAEMON <[email protected]>',  
    15.     to: '[email protected]',  
    16.     cc: '[email protected]',  
    17.     bcc: '[email protected]',  
    18.     subject: 'Reg: Send Email using node JS',  
    19.     html: 'Welcome to Node JS'  
    20.   };  
    21. transporter.sendMail(mailOptions, function (error, info) {  
    22.     if (error) {  
    23.       res.send([{  
    24.         result: "failed"  
    25.       }]);  
    26.       console.log("failed" + error);  
    27.     } else {  
    28.       res.send([{  
    29.         result: "success"  
    30.       }]);  
    31.     }  
    32.   });  
    33. });  
    34. const port = process.env.PORT || 3000;  
    35. app.listen(port);  
    36. console.log('API server started on: ' + port);  
  2. Just copy this all code and place it into a Node file and name that as Emailsending.js.
  3. Now, open the Node js Command Prompt and execute the Node.js file.
  4. For the execution of node js file, use 

    node file_name.js

  5. Sending Email Using Node.JS
  6. Once the Node.js file is executed, just check this URL http://localhost:3000/sendmail. Here, sendMail is the get method for the above program.

    Sending Email Using Node.JS


Similar Articles