How To Send Email With Attachment Using Node.js

Websolutionstuff | Aug-09-2021 | Categories : Node.js

Hello Guys,

In this tutorial we will see how to send email with attachment using node.js app. In this tutorial we will perform send mail with attachment in node.js using nodemailer module. The nodemailer module makes it easy to send emails in node.js app.

With the help of the nodemailer example, we will sending email with attachment with basic HTML content. NodeMailer is very famous and easy to use for sending email in node.js app.

You can use Mailtrap or Gmail accounts. In this tutorial, we will learn how to send email with attachment using NodeMailer with Mailtrap accounts, you can use whichever you want to.

Follow step-by-step to implement how to send email with attachment using nodemailer and mailtrap in node.js.


 Step 1 : Create Node Application

In this step create node application using below commands.

mkdir send_email_with_attachment_in_nodejs

cd send_email_with_attachment_in_nodejs

npm init



 Step 2 : Install Nodemailer

In this step install the NodeMailer module using below command

npm install nodemailer

 After you have downloaded the Nodemailer module, you can include the module in any application:

const nodemailer = require('nodemailer');

Nodemailer’s API is simple and requires following points:

  1. Create a Transporter object
  2. Create a MailOptions Object
  3. Use the Transporter.sendMail method


 Step 3 : Setup Mailtrap Account

If you don’t have a Mailtrap account, follow these steps:

  • Create Mailtrap account
  • Create new inbox
  • Get your credentials

 If already have a Mailtrap account then integrate nodemailer in SMTP Setings like below screenshots.

Mailtrap Integration



 Step 4 : Sending Email with Attachment

In this step we can send emai with attachment. So, make sure your configurarion before sending email.

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  host: "",
  port: 2525,
  auth: {
    user: "your_username",
    pass: "******"

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'How To Send Email With Attachment Using Node.js - Websolutionstuff',
  html: '<h1>Hello, This is websolutionstuff !!</h1><p>This is test mail..!</p>',
  attachments: [
            filename: 'how_to_add_summernote_editor_in_laravel.png',
            path: './uploads/how_to_add_summernote_editor_in_laravel.png'

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
  } else {
    console.log('Email sent: ' + info.response);

attachments – An array of attachments’ objects. Attachments can be used for add pdf, images, documents like csv, excel, and many more.


Step 5 : Run index.js file

run index.js using below code :

node index.js

After run this command you will get output like below screenshot.


How To Send Email with Attachment using Node.js



You may also like: 

Recommended Post
Featured Post
Line Breaks In Laravel Blade
Line Breaks In Laravel Blade

In this post, I will show you how to break lines for Textarea in laravel blade. Many times when you save...

Read More


Laravel 9 to_route() and str() Helper Function
Laravel 9 to_route() and str()...

In this article, we will see laravel 9 to_route() and str() helper function. The to_route function g...

Read More


How To Install php-bcmath In Ubuntu
How To Install php-bcmath In U...

In this article, I will guide you through the process of installing php-bcmath on Ubuntu. Php-bcmath is a PHP extension...

Read More


How To Validate Upload File Type Using Javascript
How To Validate Upload File Ty...

This article will show us how to validate upload file type using javascript. Using this post we can easily check the sel...

Read More