How To Send Email Using Node.js

In this article, we will see how to send email using node js. In this tutorial, we will see send mail in node.js using nodemailer module. The nodemailer module makes it easy to send emails in node.js, with the help of the nodemailer example, we will send emails with basic HTML content.

So, let's see how to send mail using nodemailer in node js, sending mail in node js using nodemailer, send mail using mailtrap in node js, how to send email in node js express, how to send email using nodemailer with mailtrap, node js send an email with mailtrap, how to send mail using SMTP in node js, how to send SMTP mail in node js.

NodeMailer is very famous and easy to use for sending emails in nodejs. Also, We can use Mailtrap accounts.

Step 1: Create Node JS Application

In this step, we will create a node js application using the below commands.

mkdir send_email_in_nodejs

cd send_email_in_nodejs

npm init

 

 

Step 2: Install Nodemailer Module

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.

node_js_mailtrap_integration

 

 

Step 4: Sending Email with HTML Content

In this step, we will send an email with HTML content with configuration.

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  host: "smtp.mailtrap.io",
  port: 2525,
  auth: {
    user: "your_username",
    pass: "******"
  }
});

var mailOptions = {
  from: 'from_email@gmail.com',
  to: 'to_email@gmail.com',
  subject: 'How To Send Email Using Node.js - Techsolutionstuff',
  html: '<h1>Welcome, Techsolutionstuff</h1></br><p>This is test mail..!</p>'
};

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

And if you want to send an email with text then change in mailOptions in HTML like the below code.

var mailOptions = {
  from: 'from_email@gmail.com',
  to: 'to_email@gmail.com',
  subject: 'How To Send Email Using Node.js - Techsolutionstuff',
  text: 'Welcome to Techsolutionstuff!', // Plain text body
};

 

 

Step 5: Run the index.js file

Now, run index.js using the below code.

node index.js

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS