Prompt
How do I send emails from a Node app?
Latest observation
Sep 23, 2026
OpenAI APIWeb search: off
The usual way is to use SMTP with a library like Nodemailer.
1) Install Nodemailer
npm install nodemailer
2) Create a transporter
Example using Gmail SMTP or any SMTP provider:
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for 587
auth: {
user: "your-email@example.com",
pass: "your-email-password-or-app-password",
},
});
3) Send an email
async function sendEmail() {
try {
const info = await transporter.sendMail({
from: '"My App" <your-email@example.com>',
to: "recipient@example.com",
subject: "Hello from Node",
text: "This is a plain text email.",
html: "<p>This is an <b>HTML</b> email.</p>",
});
console.log("Message sent:", info.messageId);
} catch (err) {
console.error("Error sending email:", err);
}
}
sendEmail();
4) Use environment variables
Don’t hardcode credentials:
EMAIL_USER=your-email@example.com
EMAIL_PASS=your-password
SMTP_HOST=smtp.example.com
SMTP_PORT=587
Then:
require("dotenv").config();
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
secure: false,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
Notes
- For Gmail, you usually need an App Password if 2FA is enabled.
- For production, consider services like SendGrid, Mailgun, Amazon SES, or Postmark for better deliverability.
- If you need to send lots of emails, use a queue/background job so requests don’t block.
If you want, I can give you:
- a Gmail-specific example,
- a SendGrid example, or
- a full Express API endpoint for sending mail.