发送示例
使用 Next.js 发送
从路由处理器发送,以便密钥永远不会到达浏览器。
先决条件
- 一个存储为
SENDTERN_API_KEY的Sendtern API 密钥 - A Next.js 13.4+ app using the App Router
- 1
添加密钥
将密钥添加到
.env.local,不要将其前缀为NEXT_PUBLIC_,这样会将其暴露给浏览器。1SENDTERN_API_KEY=st_your_api_key - 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
检查响应
成功的请求返回
200 OK与电子邮件 id. 使用它来查看电子邮件后.1{ 2 "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d" 3}
将示例发件人替换为您的 verified domain 上的地址。