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.
Create a project and install the package:
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:
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.
A transport is the object that knows how to reach your SMTP server. You create it once and reuse it for every message.
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:
If verify throws, the message usually tells you whether the host, port or credentials are wrong.
The sendMail method returns a promise, so use await.
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.
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.
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.
Sending can fail because of network problems, wrong credentials or a rejected recipient. Wrap the call so one failure does not crash your process.
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:
Call pooled.close() when your script ends so the process can exit.
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.
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.
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.
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.
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.
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.