コンテンツにスキップ

Nodemailer

ターミナルウィンドウ
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",
},
});

大量送信では、接続をプールします。

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 と requireTLS: true の組み合わせは STARTTLS を意味します。Nodemailer は平文で接続し、API キーを送信する前に TLS にアップグレードします。アップグレードに失敗した場合は処理を続行しません。暗黙的 TLS(secure: true、ポート 465)はまだ提供していません。

新しいコードでは、Node.js SDK をおすすめします。メールの id と、より詳しいエラーが返ります。