Tips: How to send an email using nodemailer

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Here’s how to send an email using nodemailer.

First install it:

npm install nodemailer

Then import it in your Node script or app:

import nodemailer from 'nodemailer'

Initialize a transporter object that we’ll use later to send the email:

const transporter = nodemailer.createTransport({
  host: 'smtp.yoursmtpserver.com',
  port: 465,
  secure: true,
  auth: {
    user: 'smtp_user',
    pass: 'smtp_pass',
  },
})

⚠️ NOTE: you need to fill those values with a real SMTP server credentials

Now create an options object with the details of the email you want to send:


const options = {
  from: 'flavio@blabla.com',
  to: 'flavio@yo.com',
  subject: 'Hi!',
  html: `<p>Hello</>`,
}

Finally call the sendMail() method on the transporter object you created previously, passing options and a callback that will be executed when it’s finished:


transporter.sendMail(options, (err, info) => {
  if (err) {
    console.log(err)
  } else {
    console.log('EMAIL SENT')
  }
})

This also accepts a promise-based syntax:

const info = await transporter.sendMail(options)

Full code:

import nodemailer from 'nodemailer'

const sendEmail = () => {
  const transporter = nodemailer.createTransport({
    host: 'smtp.yoursmtpserver.com',
    port: 465,
    secure: true,
    auth: {
      user: 'smtp_user',
      pass: 'smtp_pass',
    },
  })

  const options = {
    from: 'flavio@blabla.com',
    to: 'flavio@yo.com',
    subject: 'Hi!',
    html: `<p>Hello</>`,
  }

	transporter.sendMail(options, (err, info) => {
    if (err) {
      console.log(err)
    } else {
      console.log('EMAIL SENT')
    }
  })
}

Lessons in this unit:

0: Introduction
1: Axios crashes the Node.js process when the request fails
2: How to set up a cron job that runs a Node.js app
3: How to get both parsed body and raw body in Express
4: Interact with the Google Analytics API using Node.js
5: How to bulk convert file names using Node.js
6: How to deep copy JavaScript objects using structuredClone
7: How to handle file uploads in Node.js
8: ▶︎ How to send an email using nodemailer
9: Logging all the requests coming through an Express app
10: How to upload an image to S3 using Node.js
11: How to read a CSV file with Node.js
12: How to set the current working directory of a Node.js program
13: How to upload files to S3 from Node.js
14: How to write a CSV file with Node.js
15: Where to host a Node.js app
16: Parsing JSON with Node.js
17: nodemailer, how to embed an image into an email
18: The Pug Guide
19: Restarting a Node process without file changes
20: How to use Sequelize to interact with PostgreSQL