NodeJS | Send a mail with nodemailer and receive it with maildev

Web developer Front-End in Oise, France - Gary Deshayes <--Traduction

Created at : Sunday 31 October 2021

Requirements to send an email with nodemailer under NodeJS

Install nodemailer from npm :

To receive mail locally: (Useful tools to install globally)

To receive mail locally with maildev

To receive mail with maildev, you must first launch maildev after installing it globally (npm i maildev -g), then go to a command prompt to type maildev

Send the mail with nodemailer 

You will need to import nodemailer with the require command.

Then create a transporter with the development or production info (you define it) for developments we send locally to maildev (See options transporter).

Then we define the options of the mail namely, from whom the mail comes, to whom we send it, the subject, the text and HTML format: See more options

Launch the NodeJS script and the mail will be sent (node index.js in my case)

//You have to import nodemailer
const nodemailer = require('nodemailer')
//We connect to the transporter with different options depending on the environment
let transporter = null
if (process.env.NODE_ENV === 'prod') {
  transporter = nodemailer.createTransport({
    host: 'your host',
    port: 'your port',
    secure: false,
    auth: {
      user: 'the user of smtp',
      password: 'the password',
} else {
  transporter = nodemailer.createTransport({
    host: 'localhost',
    port: 1025,
    secure: false,
    tls: {
      // do not fail on invalid certs
      rejectUnauthorized: false,
// Async function to wait mail sending
async function sendMail() {
  const mailOptions = {
    from: '',
    to: '',
    subject: 'Test email for the tutorial',
    text: "Here is the text of the test email", //The texte content of the mail
      "<h1>HTML</h1><p>Here is the text in a paragraph for the test email</p>", // The html content
  await transporter.sendMail(mailOptions)
//We send mail

Viewing mail with maildev

When you launch maildev with the command prompt you see this message that tells you which port the SMTP server is pointing to and where to view the mail, so go to localhost:1080 to see the maildev interface

MailDev webapp running at
MailDev SMTP Server running at

Here is the maildev interface to receive your mails.

The tutorial is finished, thanks for reading.

News publications :

Created at : Monday 1 November 2021

Javascript Framework JS

Created at : Sunday 31 October 2021


Created at : Wednesday 1 September 2021

Javascript Framework JS

Created at : Saturday 28 November 2020

Javascript SQL Framework JS

Created at : Sunday 20 September 2020


Created at : Thursday 6 August 2020

Javascript jQuery

Created at : Monday 23 March 2020


Created at : Friday 14 February 2020

Symfony Bugs

Created at : Sunday 26 January 2020