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.

27 Comments

  1. Thelma said:

    Good way of explaining, and fastidious piece of writing to obtain facts regarding my presentation topic,
    which i am going to deliver in college.

    Take a look at my web blog – cheap flights

    January 31, 2021
    Reply
  2. Ignacio said:

    At this time it appears like BlogEngine is the best blogging platform
    out there right now. (from what I’ve read) Is that what you’re using on your blog?

    Also visit my website; cheap flights

    February 1, 2021
    Reply
  3. Dorthy said:

    This post is invaluable. When can I find out more?

    Look at my web site – cheap flights

    February 1, 2021
    Reply
  4. Chanda said:

    I’m not that much of a internet reader to be honest but your sites really nice, keep it up!

    I’ll go ahead and bookmark your website to come back in the future.
    All the best

    Here is my blog post: cheap flights [http://tinyurl.com]

    February 2, 2021
    Reply
  5. Derrick said:

    Oh my goodness! Awesome article dude! Thank you, However I am
    encountering difficulties with your RSS. I don’t understand the
    reason why I can’t join it. Is there anyone else getting
    the same RSS issues? Anyone that knows the solution will you kindly respond?
    Thanx!!

    Check out my webpage: cheap flights

    February 2, 2021
    Reply
  6. Refugio said:

    I just like the valuable info you supply for your articles.

    I’ll bookmark your blog and test once more here regularly.
    I am fairly sure I’ll be told many new stuff proper here!
    Best of luck for the following!

    Also visit my website; cheap flights (tinyurl.com)

    February 3, 2021
    Reply
  7. Aqcfgedecuddy said:

    cialis generic canada pharmacy cialis buy paypal cialis name brnd

    February 8, 2021
    Reply
  8. Kathi said:

    Hey are using WordPress for your blog platform? I’m new to the blog world
    but I’m trying to get started and set up my own. Do you
    require any coding expertise to make your own blog?
    Any help would be greatly appreciated!

    Feel free to surf to my homepage … cheap
    flights (tinyurl.com)

    February 11, 2021
    Reply

Leave a Reply

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