Ir al contenido

Nodemailer

Ventana de terminal
npm install nodemailer
mail.ts
import nodemailer from "nodemailer";
export const transport = nodemailer.createTransport({
host: "smtp.spacepost.dev",
port: 25976,
secure: false, // STARTTLS: the connection is upgraded to TLS before login
requireTLS: true,
auth: { user: "spacepost", pass: process.env.SPACEPOST_API_KEY },
});
await transport.sendMail({
from: '"Acme" <hello@acme.com>',
to: ["user@example.com"],
cc: "manager@example.com",
subject: "Your invoice",
text: "Invoice attached.",
html: '<p>Invoice attached.</p><img src="cid:logo" />',
attachments: [
{ filename: "invoice.pdf", path: "./invoice.pdf" },
{ filename: "logo.png", path: "./logo.png", cid: "logo" },
],
headers: {
"X-SpacePost-Tags": "category=invoice, order_id=1042",
"X-SpacePost-Scheduled-At": "in 1 hour",
},
});

Usa un pool de conexiones para volúmenes altos:

nodemailer.createTransport({
host: "smtp.spacepost.dev",
port: 25976,
secure: false,
requireTLS: true,
pool: true,
maxConnections: 5,
auth: { user: "spacepost", pass: process.env.SPACEPOST_API_KEY },
});

secure: false con requireTLS: true significa STARTTLS: Nodemailer se conecta en texto plano y pasa a TLS antes de enviar tu clave de API, y se niega a continuar si la actualización falla. Todavía no se ofrece TLS implícito (secure: true, puerto 465).

Para código nuevo, prefiere el SDK de Node.js: devuelve el id del correo y errores más detallados.