Wednesday, December 18, 2024
How ToProgrammingTech/Web

How to Send e-mail using NodeJS

In this tutorial i am going to discuss about How to Send e-mail using NodeJS. Sending an email in nodejs is a breeze thanks to NodeMailer. Let me walk you through the process of sending an email using NodeMailer.

Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.

Nodemailer is licensed under MIT license. See license details in the License page. If you are upgrading from Nodemailer v2 or older, then see the light migration guide here.

Nodemailer features

  • A single module with zero dependencies – code is easily auditable, as there are no dark corners
  • Heavy focus on security, no-one likes RCE vulnerabilities
  • Unicode support to use any characters, including emoji 💪
  • Windows support – you can install it with npm on Windows just like any other module, there are no compiled dependencies. Use it hassle free from Azure or from your Windows box
  • Use HTML content, as well as plain text alternative
  • Add Attachments to messages
  • Embedded image attachments for HTML content – your design does not get blocked
  • Secure email delivery using TLS/STARTTLS
  • Different transport methods in addition to the built-in SMTP support
  • Sign messages with DKIM
  • Custom Plugin support for manipulating messages
  • Sane OAuth2 authentication
  • Proxies for SMTP connections
  • ES6 code – no more unintentional memory leaks, due to hoisted var’s
  • Autogenerated email test accounts from Ethereal.email

Requirements

  • Node.js v6.0.0 or newer. That’s it.

You may like also:


How to Send e-mail using NodeJS

In this tutorial i am going to discuss about sending e-mail with Node.js. We will use Nodemailer and Gmail account so create a gmail account for this tutorial..

firstly install nodemailer in your node application.

npm install --save nodemailer

after installing the nodemailer, in your main node.js file require the nodemailer

var nodemailer = require('nodemailer');

Nodemailer needs a transport service using which it can send emails. So we use gmail.

Inside your node.js file write

var transporter = nodemailer.createTransport({
 service: 'gmail',
 auth: {
        user: 'youremail@address.com',
        pass: 'yourpassword'
    }
});

The next up is our auth object in which we have to specify our email address and password of Gmail for allowing Nodemailer to login and send email using our gmail account. Now we need a second configuration object where we will be configuring our email details.

const mailOptions = {
  from: 'sender@email.com', // sender address
  to: 'to@email.com', // list of receivers
  subject: 'Subject of your email', // Subject line
  html: '<p>Your html msg here</p>'// plain text body
};

Before sending your 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 is enabled now nodemailer can use your gmail for sending the emails.

Now the last bit is actually sending the email we can do that by using sendMail method provided by the transporter object we created above.

transporter.sendMail(mailOptions, function (err, info) {
   if(err)
     console.log(err)
   else
     console.log(info);
});

sendMail takes two argument mailOptions and a callback function which will be called when the mail is sent. The callback function will be called when either email sent successfully our an error occurred.

I hope you like this tutorial How to Send e-mail using NodeJS.


Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

2 thoughts on “How to Send e-mail using NodeJS

Leave a Reply

Your email address will not be published. Required fields are marked *