Updated on 17 Aug 2019 | 3 Min Read
By
 

Here we have mentioned some steps to send emails. If you want to send emails in Node JS then you can follow these steps. If you are a node js developer and looking for new jobs then here we have a list of top 50+ Node js interview questions and answers. These interview questions will really helpful to crack your interviews.

Send Email in node.js

Step 1. Installation

Install node mailer module with npm install nodemailer

Step 2. Include files

You have to include in var nodemailer = require('nodemailer'); your file where you want to use this.

Step 3. Use this code to send email

var transporter = mailer.createTransport({
service: 'gmail',
auth: {
   user: '[email protected]',
   pass: 'PASSWORD'
}
});

transporter.sendMail(options, function(error, info){
if (error) {
   console.log(error);
} else {
   console.log('Email sent: ' + info.response);
}
});

Here is a simple EXAMPLE of sending emails using node.js.

var mailer = require('nodemailer');

var transporter = mailer.createTransport({
service: 'gmail',
auth: {
    user: '[email protected]',
    pass: 'PASSWORD'
}
});

var options = {
    from: '[email protected]',
    to: '[email protected]',
    subject: ‘Contact Us’,
    text: 'Hello admin, you got an inquiry from contact us form!'
}

If you want to use HTML in the body then you can use simple

var options = {
     from: '[email protected]',
     to: '[email protected]',
     subject: ‘Contact Us’,
     html: '

Hello admin. You got an enquiry from contact us form!

'
}

transporter.sendMail(options, function(error, info){
if (error) {
     console.log(error);
} else {
     console.log('Email sent: ' + info.response);
}
});

 

If you want to add multiple receivers then you have to add simply. You can add multiple email address comma separated.

var options = {
      from: '[email protected]',
      to: '[email protected], [email protected]',
      subject: ‘Contact Us’,
      text: 'Hello admin, you got an inquiry from contact us form!'
}