Skip to content

All topics  /  Node.js

How to Send Email with Attachment in Node js?

Node.js ·

For teams turning “How to Send Email with Attachment in Node js?” into a repeatable delivery task, the official article can document setup, troubleshooting and review time; managers should interpret those records alongside reliability and completed work, not as a stand-alone score.

Package and operating-system behaviour can change, so verify the commands and supported versions through the Node.js project before running them on a production host.

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: '[email protected]',
        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: '[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 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='
        }
    ]
}