Skip to content

Nodemailer

Terminal window
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",
},
});

Pool connections for high volume:

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 with requireTLS: true means STARTTLS: Nodemailer connects in plain text and upgrades to TLS before it sends your API key, and refuses to continue if the upgrade fails. Implicit TLS (secure: true, port 465) is not offered yet.

Prefer the Node.js SDK for new code: it returns the email id and richer errors.