How To Send Email With Attachment Using Node.js

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 nodemailer module. The nodemailer module makes it easy to send emails in the node.js app.

So, let's see how to send attachments in the mail using node js, how to send attachments in the mail using nodemailer, send attachments in mail node.js, node.js send an email with an attachment, how to send an email with attachment using node js, nodemailer attachment example.

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

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

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

 Step 1: Create Node js Application

In this step, create a node application using the 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, we will install the NodeMailer module using the 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 the 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 settings like the below screenshots.

mailtrap_integration

 

 

 Step 4: Sending Email with Attachment

In this step, we can send emails with an attachment. So, make sure of your configuration before sending an email.

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  host: "smtp.mailtrap.io",
  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 - Techsolutionstuff',
  html: '<h1>Hello, This is techsolutionstuff !!</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) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

attachments – An array of attachments’ objects. Attachments can be used to add pdf, images, documents like CSV, Excel, and many more.

 

Step 5: Run the index.js file

Now, run index.js using the below code.

node index.js

 


You might also like: 

RECOMMENDED POSTS

FEATURE POSTS