How to Send Email with Attachment in Node js?

27-Apr-2023

.

Admin

How to Send Email with Attachment in Node js?

Hello Friends,

Today I will explain to how to send emails with file attachments in the node js project. How to send email with file attachment in node js is so easy to use. So you can just follow my step by step and learn how to send emails with file attachments in node js.

We will use how to send attachments in the mail using node js. You can see how to attach files in mail-in node js and implement a send attachment in mail-in node js.

We can send emails with attachments in the node js application. and send an email with add the file as an attachment with sending mail just follow for my few steps to create a sending mail with an attachment.

So let's start to the example.

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: 'your-email@gmail.com',

pass: 'your-gmail-password'

}

});

Once less secure apps are enabled now nodemailer can use your Gmail for sending the emails.

Step 6: Sending Email 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: 'youremail@gmail.com',

to: 'myfriend@yahoo.com',

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: 'youremail@gmail.com',

to: 'your-first-email@gmail.com, your-second-email@gmail.com',

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: 'sender@tutsmake.com',

to: 'your-email@gmail.com',

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

}

]

}

#Node JS