发送示例

使用 Node.js 发送

使用内置的 fetch 功能,从任何 Node.js 18+ 项目发送电子邮件。

先决条件

  • 一个存储为SENDTERN_API_KEY的Sendtern API 密钥
  • Node.js 18 or newer, which ships with fetch
  1. 1

    发送电子邮件

    Create send.mjs. No package is needed.

    1// send.mjs
    2const res = await fetch("https://sendtern.com/emails", {
    3  method: "POST",
    4  headers: {
    5    Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`,
    6    "Content-Type": "application/json",
    7    "Idempotency-Key": crypto.randomUUID(),
    8  },
    9  body: JSON.stringify({
    10    from: "Acme <hello@mail.example.com>",
    11    to: ["you@example.com"],
    12    subject: "Hello from Node.js",
    13    html: "<p>It works!</p>",
    14  }),
    15});
    16
    17if (!res.ok) throw new Error((await res.json()).message);
    18console.log(await res.json());
    
  2. 2

    检查响应

    成功的请求返回 200 OK 与电子邮件 id. 使用它来查看电子邮件后.

    1{
    2  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
    3}
    
1node --env-file=.env send.mjs

将示例发件人替换为您的 verified domain 上的地址。