Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Send Email From Node.js Using Nodemailer?

EmailNode.js
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
7 min read

To send email from Node.js with Nodemailer, install the package, create a transport with your SMTP server details, and call sendMail with a sender, recipient, subject and body. Nodemailer is the long-standing library for this job: it handles the SMTP conversation, TLS, attachments and HTML bodies, and it works with any provider that offers SMTP credentials.

This guide uses Nodemailer 7 on a current Node.js LTS release with ES modules. You will send a plain text email, then an HTML email with an attachment, and learn how to keep credentials out of your code.

Table of Contents

  • Installing Nodemailer and preparing credentials
  • Creating a transport
  • Sending a plain text email
  • Sending HTML with a text fallback and an attachment
  • Handling errors and closing connections
  • Testing without sending real mail
  • Frequently Asked Questions
    • Can I use Gmail with Nodemailer?
    • Why do my emails go to spam?
    • Should I send email inside the request handler?
    • Does Nodemailer work with an email API?
  • Summary
  • Related Articles

Installing Nodemailer and preparing credentials

Create a project and install the package:

bash

Add "type": "module" to package.json so you can use import syntax. You also need SMTP details from your email provider: a host name, a port, a username and a password or API key. Most providers list these on an SMTP or integration page.

Never write the password inside your source code. Put it in a .env file that you do not commit, and load it with Node's built-in flag:

text

Run your script with node --env-file=.env send.mjs. The --env-file option is built into current Node.js releases, so you do not need an extra package.

Creating a transport

A transport is the object that knows how to reach your SMTP server. You create it once and reuse it for every message.

javascript
javascript
Loading syntax highlighting...

The secure option controls how TLS starts. Port 465 uses TLS from the first byte, so secure is true. Port 587 starts as plain SMTP and upgrades with STARTTLS, so secure stays false and Nodemailer upgrades the connection when the server offers it. Port 25 is usually blocked on cloud hosts and should not be used for application mail.

You can check the configuration before sending anything:

javascript
javascript
Loading syntax highlighting...

If verify throws, the message usually tells you whether the host, port or credentials are wrong.

Sending a plain text email

The sendMail method returns a promise, so use await.

javascript
javascript
Loading syntax highlighting...
text

The from address must belong to a domain you are allowed to send from. Most providers reject or rewrite senders on domains you have not verified. accepted lists recipients the server took responsibility for, and rejected lists those it refused.

Sending HTML with a text fallback and an attachment

Real emails should carry both an HTML body and a plain text version. Clients that cannot display HTML, and spam filters that compare the two, both benefit.

javascript
javascript
Loading syntax highlighting...

Nodemailer reads the file, encodes it and sets the correct content type. You can also pass content with a string or Buffer instead of path when the file is generated in memory.

Always build HTML bodies from trusted templates. If you insert user input such as a name, escape it first so a customer cannot inject markup into your messages.

Handling errors and closing connections

Sending can fail because of network problems, wrong credentials or a rejected recipient. Wrap the call so one failure does not crash your process.

javascript
javascript
Loading syntax highlighting...

Useful error fields include code (for example EAUTH for bad credentials or ECONNECTION for network problems) and responseCode (the SMTP reply, where 4xx is temporary and 5xx is permanent).

For many messages, enable pooling so connections are reused:

javascript
javascript
Loading syntax highlighting...

Call pooled.close() when your script ends so the process can exit.

Testing without sending real mail

While developing, do not point your code at real customers. Use a local test inbox such as MailHog or Mailpit, which accepts SMTP on a local port and shows messages in a browser. Set the host to localhost, the port to the one the tool prints, and omit auth. You can also use a provider's sandbox mode if it offers one.

Frequently Asked Questions

Can I use Gmail with Nodemailer?

Yes for small personal projects, using an app password with two-step verification enabled. For application email at any volume, use a transactional email provider instead, because consumer accounts have low sending limits.

Why do my emails go to spam?

The usual causes are a sending domain without SPF, DKIM and DMARC records, a sender address that does not match the domain, or an HTML body with no text version. Fix authentication first.

Should I send email inside the request handler?

Avoid it for anything important. Put the message in a queue and send it from a background worker, so a slow SMTP server does not delay your response and failures can be retried.

Does Nodemailer work with an email API?

Nodemailer speaks SMTP. If your provider offers an HTTPS API, use its SDK or a plain fetch call, or keep Nodemailer and use the provider's SMTP credentials.

Summary

Install nodemailer, create one transport from environment variables, and call sendMail with from, to, subject, and text or html. Use port 587 or 465 with TLS, send both text and HTML, handle errors by checking code and responseCode, and test against a local inbox before you send real mail.

Related Articles

  • SMTP Relay vs Email API: Which Should Your App Use?
  • How Do You Send Transactional Email With Amazon SES?
  • Learn Email Delivery: Complete Study Roadmap, Beginner to Advanced
Share

Similar posts

  • SMTP Relay vs Email API: Which Should Your App Use?
  • How Do You Choose a Transactional Email Provider?
  • What Is Transactional Email and How Is It Different From Marketing?
  • How Do You Stop Your Mail Server Becoming an Open Relay?
  • How Do You Block Spam With SpamAssassin or Rspamd?
  • How Do You Manage and Flush the Postfix Mail Queue?
  • How Do You Read Postfix Mail Logs to Troubleshoot Delivery?
  • How Do You Configure Postfix to Relay Through a Smarthost?