How To Send Email With Attachment Using Node.js

How To Send Email With Attachment Using Node.js

Node js send an email with attachment using nodemailer; In this tutorial, you will learn how to send single or multiple receipt e-mail in node js using nodemailer with Gmail SMTP.

Note that, in node js; there are many Node.js modules available such as Nodemaileremails, express-mailer, etc for sending E-Mails.

But this tutorial will guide you step by step on how to send E-Mail and also how to send E-Mail with attachments with the Nodemailer module in node js with Gmail SMTP.

Send Email With Attachment Using Node. js

Just follow the following steps to send an email with an attachment using nodemailer in node js with Gmail SMTP:

  • Step 1 – Create Node js App
  • Step 2 – Install Nodemailer
  • Step 3 – Create App.js
  • Step 4 – Import NodeMailer Libarary in App.js
  • Step 5 – Setup Gmail SMTP driver with Nodemailer
  • Step 6 – Sending Email with Attachment
    • Send Email to Single Receipt with Attachment
    • Send Email to Multiple Receipt with Attachment
  • Step 7 – Sending Email with HTML Attachment

Step 1 – Create Node js App

Execute the following command on terminal to install node js app:

mkdir node-mail
cd node-mail
npm init -y

Step 2 – Install Nodemailer

Execute the following command on the terminal to install the Nodemailer Node.js module using NPM with the following command:

npm install nodemailer

Step 3 – Create App.js

Visit your created node js app and create a new file, which name app.js:

Step 4 – Import NodeMailer Libarary in App.js

Then, open your app.js file and import the nodemailer module in your node js application:

var nodemailer = require('nodemailer');

Step 5 – Setup Gmail SMTP driver with Nodemailer

In this step; add Gmail username and password from your selected email provider to send an email; so open your app.js file and add the following code into it:

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 are enabled now nodemailer can use your Gmail for sending the emails.

Step 6 – Sending Email with Attachment

In this step; you have two options for sending email; the options are following:

  • Send Email to Single Receipt with Attachment
  • Send Email to Multiple Receipt with Attachment

Send Email to Single Receipt with Attachment

Use the following code to send email to a single user in the node js app:

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email via Node.js',
  text: 'That was easy!'
};
 
mail.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Send Email to Multiple Receipt with Attachment

Use the following code to send email to a multiple user in the node js app:

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

mail.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Step 7 – Sending Email with HTML Attachment

If you want to send email with HTML attachment; so you can use the following code:

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='
        }
    ]
}

Conclusion

Send an email with an attachment in Node. js; In this tutorial; you have learned how to send email using nodemailer in the node js app with Gmail SMTP.

Recommended Node JS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to How To Send Email With Attachment Using Node.js

  1. Great work!!

Leave a Reply

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