Node js Send Email with Attachment Tutorial

Node js send email with attachment using nodemailer. In this tutorial, i am going to show you how to send email using the Gmail SMTP in node js with nodemailer.

Node js Send Email with Attachment Using Nodemailer

  • Step 1 – Create Node js App
  • Step 2 – Install Nodemailer
  • Step 3 – Create App.js
  • Step 4 – Import NodeMailer Libarary in Server.js
  • Step 5 – Add 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

Open command prompt and run the following command on terminal to install node js app:

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

Step 2 – Install Nodemailer

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

npm install nodemailer

Step 3 – Create Server.js

Go to your created node js app send email with attachment app root directory and create a new file, which name server.js.

Step 4 – Import NodeMailer Libarary in Server.js

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

var nodemailer = require('nodemailer');

Step 5 – Add Gmail SMTP driver with Nodemailer

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'
  }
});

Note that, sending email using Gmail SMPT; 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

There are 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!'
};
  
transporter.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!'
}
 
transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Step 7 – Sending Email with HTML Attachment

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

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

Recommended Node js Tutorials

Leave a Comment