How to send Emails in Node.js

How to send Emails in Node.js

In this tutorial, we will learn “How to send Emails in Node.js using Nodemailer“.

How to send Emails using NodeMailer?

Sending an email in the node is very easy thanks to NodeMailer. Let me show you the process of sending an email using it.

I assume that you have already running your node.js application or you can learn how to start with the node.js application from my previous tutorial here.

First, install NodeMailer to your node application

npm install --save nodemailer

After installing in your js file require it

const nodeMailer = require('nodemailer');

Now, create transporter object using the default SMTP transport which can send emails. In this example, I am using ZOHO mail.

let transporter = nodeMailer.createTransport({
            host: 'smtp.zoho.com',
            port: 465,
            secure: true,  //true for 465 port, false for other ports
            auth: {
                user: '[email protected]',
                pass: 'password'
            }
        });

In auth object specify your email address and password.

Now, we need to configure our email details.

// setup email data with unicode symbols
        let mailOptions = {
            from: '"Your Name" <[email protected]>', // sender address
            to: '[email protected], [email protected]', // list of receivers
            subject: 'Hello ✔', // Subject line
            text: 'Hello world?', // plain text body
            html: '<b>Hello world?</b>' // html body
        };

Now the last thing is actually sending the email using ‘sendMail()’ method provided by the transporter object we created.

transporter.sendMail(mailOptions, (error, info) => {if (error) {console.log(error);res.status(400).send({success: false})} else {res.status(200).send({success: true});}});

‘sendMail()’ method takes two argument mailOptions and a callback function which will be called when the mail is sent. The callback function will be called when either email sent successfully or an error occurred.

Putting everything together,

'use strict';

const nodeMailer = require('nodemailer');

exports.sendMail = function(req,res){
  const transporter = nodeMailer.createTransport({
    host: 'smtp.zoho.com',
    port: 465,
    secure: true,  //true for 465 port, false for other ports
    auth: {
      user: '[email protected]',
      pass: 'password'
    }
  });

  const mailOptions = {
    from: '"Your Name" <[email protected]>', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: 'Hello ', // Subject line
    text: 'Hello world?', // plain text body
    html: '<b>Hello world?</b>' // html body
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error);
      res.status(400).send({success: false})
    } else {
      res.status(200).send({success: true});
    }
  });
}

And that’s how we send emails using NodeMailer.

Github link: https://goo.gl/X9dpHx

Here are more tutorials:

Any doubts, feel free to comment.

Happy Coding!!Cheers!!

Adam Brown
Please follow and like us:

Leave a Comment