Pular para o conteúdo

Nodemailer

Janela do 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",
},
});

Use um pool de conexões para volumes 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 com requireTLS: true significa STARTTLS: o Nodemailer se conecta em texto puro e faz o upgrade para TLS antes de enviar a sua chave de API, e se recusa a continuar se o upgrade falhar. O TLS implícito (secure: true, porta 465) ainda não é oferecido.

Prefira o SDK para Node.js em código novo: ele retorna o id do e-mail e erros mais detalhados.