Home > Mail > Sending the emails with attachment in Node.js

Sending the emails with attachment in Node.js

In the earlier post we have discussed about Sending HTML template emails using SMTP in NodeJS , now we will learn about how to sending the emails with attachment in Node.js along with with HTML template.

Sending the emails with attachment in Node.js by Anil Labs

Sending the emails with attachment in Node.js by Anil Labs

We have already learn and discussed about the node module and how to integrate module to your file. In this we will extend the code.

Earlier mailOptions is like

var mailOptions = {
from: mailset.FromName+'<'+mailset.From+'>', // sender address
to: toemail, // list of receivers
subject: 'Sending HTML template mails using SMTP in NodeJS', // Subject line
html: data // html body
};

Add the below code to attachment the external file

attachments: [
{ //using URL as an attachment
filename: 'anillabs.pdf', // File name in the email
filePath: [attachmentUrl]
},
]

Now mailOptions will changed to

var mailOptions = {
from: mailset.FromName+'<'+mailset.From+'>', // sender address
to: toemail, // list of receivers
subject: 'Sending HTML template email with attachment in NodeJS', // Subject line
html: data, // html body
attachments: [
{ //using URL as an attachment
filename: 'anillabs.pdf', // File name in the email
filePath: [attachmentUrl]
},
]
};

We can multiple attachments to email. Hope it will be useful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.