How To Send Mail In NodeJS

Posted By : Rajat khurana | 26-Sep-2017

Nodemailer is a module for Node.js applications to send email using Gmail or any email providing service like Mailgun etc. But to use Gmail you may need to configure "Allow Less Secure Apps" in your Gmail account ( configure gmail account ) unless you never able to send email. So, it's better to send mail using Mail Services like MailGun where a sandbox is created to receive mail.
 
To configure it with Gmail, you need to specify host i.e smtp.gmail.com.

 
transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 447, // Gmail Port
        auth: {
            user: account.user, // Gmail id
            pass: account.pass  // Gmail password
        }
    });


var mailOptions = {
    from:  // sender address
    to: // list of receivers
    subject: // Subject line
    text:  // plaintext body
    html:  // html body
};

// send mail with defined transport object

transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

Sendmail function will send mail based on mail options. If you are using some email service then you need to change transporter object.

Thanks

 

About Author

Author Image
Rajat khurana

Rajat is a bright Javascript Developer, he has good knowledge of HTML, WordPress, NodeJs, Core Java, Javascript, Jquery. Apart from this he loves to play Badminton and PC Gaming.

Request for Proposal

Name is required

Comment is required

Sending message..