FOLASAYO SAMUEL OLAYEMI
SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

Follow

SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

Follow
How to send an Email in NodeJS, and how you can receive and read mail with IMAP instead of sending.

Photo by Clark Tibbs on Unsplash

How to send an Email in NodeJS, and how you can receive and read mail with IMAP instead of sending.

FOLASAYO SAMUEL OLAYEMI's photo
FOLASAYO SAMUEL OLAYEMI
Β·Apr 8, 2022Β·

3 min read

Play this article

Table of contents

What is NodeMailer Module?

Nodemailer is a module for Node.js applications that makes it easy to send emails from your computer.

Why NodeMailer?

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.

How can I Install NodeMailer?

You can install NodeMailer in your Node.js application using:

npm install nodemailer

You can now include the module in your application like this:

const nodemailer = require('nodemailer');

How to send an Email with NodeMailer from your application

You can send emails from your server, by using the username and password from your selected email provider to send an email. The example below explained how to use your Gmail account to send an email.

Code Sample:

const nodemailer = require('nodemailer');

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

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

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

Now your server is able to send emails.

How can I email Multiple Receivers

To email more than one receiver, add them to the "to" property of the mailOptions object, separated by commas.

Code Sample:

const mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com, myotherfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
}

How can you send an HTML formatted text in your email?

To send HTML formatted text in your email, use the "html" property instead of the "text" property.

Code Sample:

const mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  html: '<h1>Welcome</h1><p>That was easy!</p>'
}

Did you want to receive and read mail with IMAP instead of sending?

You can check out EmailEngine – self-hosted service that allows to make REST request against IMAP and SMTP servers. EmailEngine also sends webhooks whenever something changes on the registered accounts.

  • Here are some NodeMailer features

  • A single module with zero dependencies – code is easily auditable, as there are no dark corners.

  • Heavy focus on security.

  • 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

What are the requirements to be able to use NodeMailer in your Node.js application

If you are able to run Node.js version 6 or newer, then you can use Nodemailer. There are no platform or resource specific requirements. All public Nodemailer methods support both callbacks and Promises (if callback is omitted). You need to have at least Node v8.0.0 if you want to use async..await with Nodemailer.

Thanks for reading...

Happy Coding!

Did you find this article valuable?

Support FOLASAYO SAMUEL OLAYEMI by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Β 
Share this