Skip to content

Sending Email from Node with Nodemailer

Beginning of the lesson

Sending Email from Node with Nodemailer

Deliver welcome messages, password resets, and notifications from your Node backend

Big idea: Almost every real application eventually needs to send transactional email — a message triggered by a user action, such as a welcome note on sign-up, a password-reset link, or an order receipt. Nodemailer is the de-facto Node library for this: a single, dependency-light module that connects to an SMTP server and sends fully formed messages with HTML bodies, plain-text fallbacks, and attachments.
By the end you will be able to:
  • create an SMTP transport and send your first message with sendMail();
  • build a message object with to, from, subject, and both text and html bodies;
  • attach files and embed inline images with cid;
  • keep SMTP credentials out of your code using environment config;
  • test sends during development without spamming real inboxes.

1. What "transactional email" means

There are two broad kinds of email a backend sends. Marketing email (newsletters, promotions) goes to many recipients at once and is usually handled by a dedicated platform. Transactional email is different: it is a single message sent to one person because of something they just did. A user registers, so you send a welcome email. A user clicks "forgot password", so you send a reset link. An order ships, so you send a confirmation. These are the messages Nodemailer is perfect for.

Analogy. Think of your application as a busy office. Transactional email is the office assistant who, the instant a form lands on the desk, writes a tidy reply and drops it in the outgoing mail tray. Nodemailer is that assistant: you hand it the details of one letter, and it formats the envelope, addresses it, and passes it to the postal service (the SMTP server) for delivery.

Nodemailer itself does not deliver mail across the internet — that is the job of an SMTP server (Simple Mail Transfer Protocol). Nodemailer's job is to build a correct message and speak SMTP to a server you choose, whether that is Gmail, a company mail server, or a provider such as SendGrid, Mailgun, or Amazon SES exposed over SMTP.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers