How to send Attachments and Email using Node js?

Jan 09, 2023

.

Admin

How to send Attachments and Email using Node js?

Hello Friends,

In this article, we will see how to send emails with attachments using the node.js app. In this tutorial, we will perform send a mail with an attachment in node.js using the nodemailer module. The nodemailer module makes it easy to send emails in the node.js app.

Adding an email messaging API to your mobile or web app is a necessity as it provides a channel of communication between you and your end-users. Email messages are used for transactional purposes, notifications, advertisement, etc.

Step 1: Create Node js App


Execute the following command on the terminal to install the 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 is 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 a 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'

}

});

Before sending an 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 to send 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 Receipts with Attachment
  • Send Email to Single Receipt with Attachment

    Use the following code to send an 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 Receipts with Attachment

    Use the following code to send email to a multiple users 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@nicesnipets.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='

    }]

    }

    I hope it can help you...

    #Node JS