边缘函数

发送电子邮件


使用 Resend API 从边缘函数发送电子邮件

前置准备

为了充分利用本指南,您需要:

确保已安装最新版本的 Supabase CLI

1. 创建 Supabase 函数

在本地创建新函数:

1
supabase functions new resend

RESEND_API_KEY 存储在您的 .env 文件中。

2. 编辑处理函数

将以下代码粘贴到 index.ts 文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY')const handler = async (_request: Request): Promise<Response> => { const res = await fetch('https://api.resend.com/emails', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${RESEND_API_KEY}`, }, body: JSON.stringify({ from: 'onboarding@resend.dev', to: 'delivered@resend.dev', subject: 'hello world', html: '<strong>it works!</strong>', }), }) const data = await res.json() return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json', }, })}Deno.serve(handler)

3. 部署并发送邮件

在本地运行函数:

1
2
supabase startsupabase functions serve --no-verify-jwt --env-file .env

测试地址:http://localhost:54321/functions/v1/resend

将函数部署到 Supabase:

1
supabase functions deploy resend --no-verify-jwt

打开端点 URL 发送电子邮件:

4. 亲自尝试

GitHub 上查看完整示例。