通过 HTTP 接口配置 Cloudflare 电子邮箱路由:创建自定义地址、动作为"发送到电子邮箱"、并指定目标邮箱。
cf_email_routing.py— 封装 Cloudflare API 的 SDK 类config.py— 读取config.yaml中的敏感信息api_server.py— 基于 FastAPI 的 HTTP 服务config.example.yaml— 配置示例,复制为config.yaml后填写
pip install -r requirements.txt
copy config.example.yaml config.yaml
# 编辑 config.yaml 填写 api_token / account_id / access_key
python api_server.py服务启动后打开 http://127.0.0.1:8000/docs 查看 Swagger 文档并在线调试。
所有 /api/** 请求必须在请求头添加:
X-API-Key: <config.yaml 中的 api.access_key>
curl -X POST http://127.0.0.1:8000/api/routing/rules `
-H "X-API-Key: your_access_key" `
-H "Content-Type: application/json" `
-d '{
"domain": "example.com",
"custom_address": "hello@example.com",
"destination": "your_mailbox@gmail.com"
}'- 域名需先在 Cloudflare 启用 Email Routing(添加 MX/SPF 记录)。
- 目标邮箱必须先通过 Cloudflare 的验证邮件,规则才能生效。
- API Token 至少需要权限:
Zone → Email Routing Rules → EditAccount → Email Routing Addresses → Edit(如果要新增目标邮箱)
- 请妥善保管
config.yaml,不要提交到版本库。