Sending Emails Through Node js

How to send Emails through node Js. Sending the server-mails has been very popular and used in production-based applications since long now. We can send simple text format email to multiple users through any email service or we can send HTML template mails for promotions and adds. We will Use the Nodemailer package for this task.

What is Nodemailer?

⇒ Nodemailer is a 📦module/package for node js application which allows easy ways to send the individual/group emails through the server. This package is very easy to configure and can be installed using the following command in the command line with the root directory of the project open.

npm install nodemailer

⇒This uses the standard SMTP service to deliver the message. As SMPT protocol is used between different host there is no single problem in sending emails to different email services. Example:- If the sender email is of Gmail service and we will be sending emails through this then receiver can receive they mail weather they are using Gmail service or not.

⇒Nodemailer package is very big with multiple options and services. The major functions of nodemailer that are the core of sending emails are createtransporter() and sendmail().

Steps:-

  1. Create a file app.js in the root directory and require the nodemailer package.
  2. Create a variable as ‘transport’ and use createtransport() function and pass on the object parameter with details of email service used, username/email and password for that account.
  3. Create an object variable which has all the basic information of who is sending email and the receivers email address, the subject of the email, text data or message and HTML template if any.
  4. Used sendmail() function pass on 2 parameters, first is the object variable created earlier and second is callback consist of error and info(data send).
  5. Pass them through the conditional if-else and console the success and failure message.
  6. And use command Node app.js to run the program.
    var nodemailer = require('nodemailer');
    
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'xyz@gmail.com',
        pass: 'xxxx'
      }
    });
    
    var mailOptions = {
      from: 'xyz@gmail.com',
      to: 'REciver@gmail.com',
      subject: 'Sending Email using Node.js',
      text: `Hello  sir this email is sent You through the node js system and work awsomely.`
      // html: '<h1>Hi USer</h1><p>Your Messsage</p>'        
    };
    
    transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    });

    Note- 1) The email and password used here are dummies(as for privacy reason) and you can use valid email id and password. We can use the environment variables for email id and password.

    2) Give permission to email service to send email through server. Link to grant permission  Click Here!

Output:- The email received and callback success message