送出例子

用Next.js發送

從路由處理器發送,以便密鑰永遠不會到達瀏覽器。

前提條件

  • 存儲為 SENDTERN_API_KEY 的 Sendtern API 鍵
  • A Next.js 13.4+ app using the App Router
  1. 1

    加入密鑰

    將密鑰加入 .env.local。不要將它前綴為 NEXT_PUBLIC_,否則會讓瀏覽器看到它。

    1SENDTERN_API_KEY=st_your_api_key
    
  2. 2

    Create a Route Handler

    Create app/api/send/route.js. It runs on the server only.

    1// app/api/send/route.js
    2export async function POST(request) {
    3  const { email } = await request.json();
    4
    5  const res = await fetch("https://sendtern.com/emails", {
    6    method: "POST",
    7    headers: {
    8      Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`,
    9      "Content-Type": "application/json",
    10    },
    11    body: JSON.stringify({
    12      from: "Acme <hello@mail.example.com>",
    13      to: [email],
    14      subject: "Welcome to Acme",
    15      html: "<p>Thanks for signing up.</p>",
    16    }),
    17  });
    18
    19  return Response.json(await res.json(), { status: res.status });
    20}
    
  3. 3

    檢查一下反應

    成功的請求返回 200 OK 與電子郵件 ID。使用它以後查看電子郵件。

    1{
    2  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
    3}
    

用您的 verified domain 的地址替换示例发件人。