Node js Send Email Through Gmail with Attachment Example
Nosql 09-Mar-2021

Node js Send Email Through Gmail with Attachment Example

Overview

In this node js email sends tutorial, you will learn how you can send the email using the Gmail SMTP in node js. Here you will learn step by step, how you can send email using Gmail SMTP in node js

Sending email via Node js is easy. Today we are going to discuss send an email via node js. We will use nodemailermodule and Gmail SMTP to send the email. We will also learn how to send an email with an attachment. So let’s get started with the node js send email with attachment tutorial

First Install Nodemailer

npm install nodemailer

Next, include the nodemailer module in your application

var nodemailer = require('nodemailer');

Configure Gmail SMTP with Nodemailer

The nodemailer needs a transport service using which it can send emails. Use the username and password from your selected email provider to send an email. This tutorial will show you how to use your Gmail account to send an email:

var mail = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your-email@gmail.com',
    pass: 'your-gmail-password'
  }
});

Before sending email using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled now nodemailer can use your Gmail for sending the emails.

Sending Email with Gmail SMTP

Now you are ready to send emails from your server.

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email via Node.js',
  text: 'That was easy!'
};
  
transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Send Multiple Recipient At The Same Time

Let’s see, how you can send email to multiple users at the same time

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'your-first-email@gmail.com, your-second-email@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
}

Send Simple HTML

To send HTML formatted text in your email, use the “html” property instead of the “text” property:

var mailOptions = {
   from: 'youremail@gmail.com',
   to: 'your-first-email@gmail.com, your-second-email@gmail.com',
   subject: 'Sending Email using Node.js',
   html: '<h1>Welcome</h1><p>That was easy!</p>'
 }

Send Attachment

attachments option in the message object that contains an array of attachment objects.

var mailOptions = {
  from: 'sender@tutsmake.com',
  to: 'your-email@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!',
  attachments: [{   // utf-8 string as an attachment
            filename: 'text1.txt',
            content: 'hello world!'
        },
        {   // binary buffer as an attachment
            filename: 'text2.txt',
            content: new Buffer('hello world!','utf-8')
        },
        {   // file on disk as an attachment
            filename: 'text3.txt',
            path: '/path/to/file.txt' // stream this file
        },
        {   // filename and content type is derived from path
            path: '/path/to/file.txt'
        },
        {   // stream as an attachment
            filename: 'text4.txt',
            content: fs.createReadStream('file.txt')
        },
        {   // define custom content type for the attachment
            filename: 'text.bin',
            content: 'hello world!',
            contentType: 'text/plain'
        },
        {   // use URL as an attachment
            filename: 'license.txt',
            path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE'
        },
        {   // encoded string as an attachment
            filename: 'text1.txt',
            content: 'aGVsbG8gd29ybGQh',
            encoding: 'base64'
        },
        {   // data uri as an attachment
            path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
        }
    ]
}

After the above changes our app.js the file will look like this

var nodemailer = require('nodemailer');
  
var mail = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your-email@gmail.com',
    pass: 'your-gmail-password'
  }
});
  
var mailOptions = {
   from: 'youremail@gmail.com',
   to: 'your-first-email@gmail.com, your-second-email@gmail.com',
   subject: 'Sending Email using Node.js',
   html: '<h1>Welcome</h1><p>That was easy!</p>' ,
   attachments: [{
       filename: 'text1.txt',
       content: 'hello world!'
   }
}
  
mail.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
});

If you want to know more about the nodemailer click here