Authentication & Security
Node.js Sending Emails with Nodemailer
Nodemailer sends email from Node over SMTP or a transactional provider. It handles HTML bodies, attachments, connection pooling and authentication including OAuth2.
What is Sending Emails with Nodemailer in Node.js?
Nodemailer sends email from Node over SMTP or a transactional provider. It handles HTML bodies, attachments, connection pooling and authentication including OAuth2.
Sending Emails with Nodemailer example
JavaScript
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 587,
secure: false,
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
pool: true,
maxConnections: 3,
});
export async function sendWelcome(to, name) {
await transporter.sendMail({
from: '"MyInternships" <no-reply@example.com>',
to,
subject: 'Welcome aboard',
text: `Hi ${name}, welcome!`, // plain-text fallback matters for deliverability
html: `<p>Hi <b>${name}</b>, welcome!</p>`,
});
}Key points to remember
- Create the transporter once at module level, not per email.
- Deliverability depends on SPF, DKIM and DMARC DNS records more than on code.
- Send email in the background — never make a user wait for SMTP.
- Always include a plain-text alternative alongside the HTML.
Common mistakes with Sending Emails with Nodemailer
- Awaiting the send inside a signup request, adding seconds to the response.
- Missing SPF and DKIM records, so mail lands in spam regardless of the code.
- Sending in a tight loop with high concurrency, which many SMTP servers throttle or drop.
Node.js Sending Emails with Nodemailer— Interview Questions & FAQs
Why do my Nodemailer emails go to spam?+
Almost always DNS rather than code — missing or misconfigured SPF, DKIM and DMARC records for the sending domain. Verify them before changing anything in the application.
