Nodemailer Documentation
website·Indexed 39 days ago
https://nodemailer.comA module for Node.js applications to send emails. Features include support for SMTP, Amazon SES, Sendmail, and Stream transports, as well as DKIM signing, OAuth2 authentication, and a streaming email parser called MailParser. The documentation covers message configuration, transport options, and guides for integrating with services like Gmail and Ethereal.
What's inside Nodemailer
- Ethereal is a free fake SMTP service designed for testing Nodemailer and other email-sending applications. It captures sent messages and displays them in a web interface, but ensures they are never delivered to real recipients. It is ideal for development and debugging because it requires no signup and provides instant previews of emails.
Embed iCalendar events using the icalEvent option
Nodemailer allows embedding an iCalendar (
.ics) file directly in an email. This enables interactive controls like 'Add to calendar' or 'Accept/Decline' in clients like Gmail, Outlook, and Apple Mail.Important: Nodemailer does not generate the
.icscontent. You must use a library such asical-generatororicsto create the valid iCalendar data before passing it to Nodemailer.For maximum compatibility across email clients, it is recommended to keep the message simple: include only
text,html, and a singleicalEvent. Avoid adding other file attachments to calendar invitation emails.Validate email authentication with mailauth
Themailauthlibrary provides tools to validate and generate email authentication records, including SPF, DKIM, DMARC, ARC, and BIMI, to improve email deliverability and verify authenticity.Manage emails via IMAP with ImapFlow
ImapFlow is a modern, Promise-based IMAP client for Node.js used for reading and managing emails on any IMAP server.Understand the Nodemailer SMTP response object
When using the SMTP transport, thesendMailmethod returns an info object containing details about the delivery. Note that a message is considered 'sent' if at least one recipient was accepted; you must check therejectedarray to identify partial failures.// Properties available in the SMTP response object { messageId: string, // The Message-ID header value envelope: object, // SMTP envelope addresses (from and to) accepted: string[], // Recipient addresses accepted by the server rejected: string[], // Recipient addresses rejected by the server rejectedErrors: any[], // Error details for rejected recipients response: string, // Final response string from SMTP server ehlo: string[], // Server capability lines from EHLO response envelopeTime: number, // ms spent on envelope exchange messageTime: number, // ms spent streaming message body messageSize: number // Size of transferred message in bytes }Use email alternatives to provide multiple body formats
Alternatives are different representations of the same email message (e.g., Markdown, machine-readable formats). Unlike attachments, which are downloadable files, alternatives are versions of the email body itself. The recipient's email client automatically selects and displays the best format it supports from a
multipart/alternativecontainer.For calendar invitations, use the dedicated
icalEventoption instead of thealternativesarray for better compatibility.Understand Nodemailer plugin lifecycle phases
Nodemailer allows injecting custom logic at three distinct phases of a message's lifecycle:
Phase Stage When it runs Typical uses Pre-processing compileAfter the message object is created but before the MIME source is generated Templating, automatic plain-text alternatives, address validation Processing streamAfter MIME compilation, while the message stream is being prepared for sending Inlining images, transforming HTML content Sending transportWhen the message is ready to be delivered Custom HTTP APIs, SMTP, SES, SparkPost Note:
compileandstreamplugins are registered viatransporter.use(). Thetransportstage is not registered withuse(); instead, a custom transport object is passed tonodemailer.createTransport().Manage SMTP sessions with SMTPConnection
TheSMTPConnectionmodule is a low-level SMTP client used for establishing outbound SMTP connections. It is the foundation for Nodemailer's SMTP transport and is used when fine-grained control over the SMTP session lifecycle is required. It is included with Nodemailer and requires no additional installation.Reference the SMTPServer Session object
The
sessionobject is passed to all callback handlers and contains metadata about the current connection:Property Type Description idString Unique identifier for the connection. remoteAddressString Client's IP address. clientHostnameString Reverse DNS hostname of the client. openingCommandString The greeting command sent ( HELO,EHLO, orLHLO).hostNameAppearsAsString Hostname provided by the client in the greeting. envelopeObject Mail transaction data (mailFrom, rcptTo). userany Value returned from onAuthafter successful login.transactionNumber Transaction counter (1 for first message, etc.). transmissionTypeString Type string for Received: headers (e.g., SMTP,ESMTP).Build an SMTP server with smtp-server
Use thesmtp-serverlibrary to build a custom SMTP server in Node.js that can accept incoming email connections. This is ideal for creating custom mail servers, testing email workflows, or building applications that receive email.Turn email accounts into REST endpoints with EmailEngine
EmailEngine is a self-hosted, commercial, source-available email API that converts Gmail, Microsoft 365, or IMAP accounts into REST endpoints. It handles OAuth2, token refresh, SMTP sending, and provides real-time updates via webhooks.Generate RFC 822 email messages with mailcomposer
Themailcomposerlibrary generates RFC 822-compliant email messages from JavaScript objects. This is useful for creating properly formatted MIME messages that are not sent immediately, such as for storing drafts or passing to another system. Note: This library ships inside the Nodemailer package.