In Node JS, NodeMailer is simple library that helps in sending mails in Node JS applications with attachments. In this tutorial, you will learn how to send emails in node js using nodemailer with Gmail SMTP.
Send Email With Attachment Using Node. js
Here are steps to send mail with an attachment using nodemailer 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
Run the following command on cmd to create new node js app:
mkdir node-mail cd node-mail npm init -y
Step 2 – Install Nodemailer
Run the following command on the cmd to install the Nodemailer module in application:
npm install nodemailer
Step 3 – Create App.js
Navigate to your created node js application directory and create a new file named app.js
.
Step 4 – Import NodeMailer Library in App.js
Edit your app.js
file and import the nodemailer
module in it:
var nodemailer = require('nodemailer');
Step 5 – Setup Gmail SMTP driver
Set up Gmail username and password from your selected email provider to send an email in app.js
file:
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 emails; 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 an email with an HTML attachment, 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
In this tutorial; you have learned how to send email using nodemailer in the node js app with Gmail SMTP.
Great work!!