Node js send email with attachment

Sending email via Node js is super easy. Today we are going to discuss about send email via nodejs. We will use nodemailer module amd gmail smtp to send the email. We will also learn how to send email with attachment. So let’s get started with the node js send email with attachment tutorial

Installing Nodemailer

npm install nodemailer

Next, include the nodemailer module in your application

var nodemailer = require('nodemailer');

Configure Nodemailer with Gmail

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: '[email protected]',
    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 is enabled now nodemailer can use your gmail for sending the emails.

Sending Email

Now you are ready to send emails from your server.

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  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);
  }
});

Multiple Receivers

Send email to multiple user at same time

var mailOptions = {
  from: '[email protected]',
  to: '[email protected], [email protected]',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
}

Send HTML

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

var mailOptions = {
   from: '[email protected]',
   to: '[email protected], [email protected]',
   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: '[email protected]',
  to: '[email protected]',
  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 above changes our app.js file will looks like this

var nodemailer = require('nodemailer');

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

var mailOptions = {
   from: '[email protected]',
   to: '[email protected], [email protected]',
   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);
      }
});

Click here to learn more about the nodemailer

If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *