当请求方携带与服务端环境变量 JWT_TOKEN 完全一致的令牌时,将跳过会话 Cookie/JWT 校验,直接被识别为最高管理员(strictAdmin)。
配置项:
wrangler.toml→[vars]→JWT_TOKEN="你的超管令牌"
令牌携带方式(任选其一):
- Header(标准):
Authorization: Bearer <JWT_TOKEN> - Header(自定义):
X-Admin-Token: <JWT_TOKEN> - Query:
?admin_token=<JWT_TOKEN>
生效范围:
- 所有受保护的后端接口:
/api/* - 会话检查:
GET /api/session - 收信回调:
POST /receive - 管理页服务端访问判定(
/admin//admin.html)与未知路径的认证判断
行为说明:
- 命中令牌后,鉴权载荷为:
{ role: 'admin', username: '__root__', userId: 0 } strictAdmin判定对__root__为 true(与严格管理员等价)- 若未携带或不匹配,则回退到原有 Cookie/JWT 会话验证
使用示例:
# Authorization 头
curl -H "Authorization: Bearer <JWT_TOKEN>" https://your.domain/api/mailboxes
# X-Admin-Token 头
curl -H "X-Admin-Token: <JWT_TOKEN>" https://your.domain/api/domains
# Query 参数
curl "https://your.domain/api/session?admin_token=<JWT_TOKEN>"安全提示: 严格保密 JWT_TOKEN,并定期更换。
| 角色 | 说明 |
|---|---|
strictAdmin |
最高管理员,完全系统访问权限 |
admin |
管理员,用户管理和邮箱控制 |
user |
普通用户,只能管理分配的邮箱 |
mailbox |
邮箱用户,只能访问自己的单个邮箱 |
guest |
访客,只读模拟数据 |
用户登录
请求参数:
{
"username": "用户名或邮箱地址",
"password": "密码"
}支持的登录方式:
- 管理员登录:使用
ADMIN_NAME/ADMIN_PASSWORD环境变量 - 访客登录:用户名
guest,密码为GUEST_PASSWORD环境变量 - 普通用户登录:数据库
users表中的用户 - 邮箱登录:使用邮箱地址登录(需启用
can_login)
返回示例:
{
"success": true,
"role": "admin",
"can_send": 1,
"mailbox_limit": 9999
}用户退出登录
返回:
{ "success": true }验证当前会话状态
返回:
{
"authenticated": true,
"role": "admin",
"username": "admin",
"strictAdmin": true
}获取可用域名列表
返回:
["example.com", "mail.example.com"]随机生成新的临时邮箱
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
length |
number | 可选,随机字符串长度 |
domainIndex |
number | 可选,选择域名索引(默认 0) |
返回:
{
"email": "abc123@example.com",
"expires": 1704067200000
}自定义创建邮箱
请求参数:
{
"local": "myname",
"domainIndex": 0
}返回:
{
"email": "myname@example.com",
"expires": 1704067200000
}获取当前用户的邮箱列表
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
limit |
number | 分页大小(默认 100,最大 500) |
offset |
number | 偏移量 |
domain |
string | 按域名筛选 |
favorite |
boolean | 按收藏状态筛选 |
forward |
boolean | 按转发状态筛选 |
返回:
[
{
"id": 1,
"address": "test@example.com",
"created_at": "2024-01-01 00:00:00",
"is_pinned": 1,
"password_is_default": 1,
"can_login": 0,
"forward_to": "backup@gmail.com",
"is_favorite": 1
}
]删除指定邮箱
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string | 要删除的邮箱地址 |
返回:
{ "success": true, "deleted": true }获取当前用户的邮箱配额
返回(普通用户):
{
"limit": 10,
"used": 3,
"remaining": 7
}返回(管理员):
{
"limit": -1,
"used": 150,
"remaining": -1,
"note": "管理员无邮箱数量限制"
}切换邮箱置顶状态
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string | 邮箱地址 |
返回:
{ "success": true, "pinned": true }重置邮箱密码(仅 strictAdmin)
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string | 邮箱地址 |
返回:
{ "success": true }切换邮箱登录权限(仅 strictAdmin)
请求参数:
{
"address": "test@example.com",
"can_login": true
}返回:
{ "success": true, "can_login": true }修改邮箱密码(仅 strictAdmin)
请求参数:
{
"address": "test@example.com",
"new_password": "newpassword123"
}返回:
{ "success": true }批量切换邮箱登录权限(仅 strictAdmin)
请求参数:
{
"addresses": ["test1@example.com", "test2@example.com"],
"can_login": true
}返回:
{
"success": true,
"success_count": 2,
"fail_count": 0,
"total": 2,
"results": [
{ "address": "test1@example.com", "success": true, "updated": true }
]
}设置邮箱转发地址
请求参数:
{
"mailbox_id": 1,
"forward_to": "backup@gmail.com"
}返回:
{ "success": true }切换邮箱收藏状态
请求参数:
{
"mailbox_id": 1,
"is_favorite": true
}返回:
{ "success": true }批量设置收藏(按 ID,仅 strictAdmin)
请求参数:
{
"mailbox_ids": [1, 2, 3],
"is_favorite": true
}批量设置转发(按 ID,仅 strictAdmin)
请求参数:
{
"mailbox_ids": [1, 2, 3],
"forward_to": "backup@gmail.com"
}批量设置收藏(按地址,仅 strictAdmin)
请求参数:
{
"addresses": ["test1@example.com", "test2@example.com"],
"is_favorite": true
}批量设置转发(按地址,仅 strictAdmin)
请求参数:
{
"addresses": ["test1@example.com", "test2@example.com"],
"forward_to": "backup@gmail.com"
}邮箱用户修改自己的密码
请求参数:
{
"currentPassword": "oldpassword",
"newPassword": "newpassword123"
}返回:
{ "success": true, "message": "密码修改成功" }获取邮件列表
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
mailbox |
string | 邮箱地址(必需) |
limit |
number | 返回数量(默认 20,最大 50) |
返回:
[
{
"id": 1,
"sender": "sender@example.com",
"subject": "邮件主题",
"received_at": "2024-01-01 12:00:00",
"is_read": 0,
"preview": "邮件内容预览...",
"verification_code": "123456"
}
]批量获取邮件元数据
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
ids |
string | 逗号分隔的邮件 ID(最多 50 个) |
返回:
[
{
"id": 1,
"sender": "sender@example.com",
"to_addrs": "recipient@example.com",
"subject": "邮件主题",
"verification_code": "123456",
"preview": "预览...",
"r2_bucket": "mail-eml",
"r2_object_key": "2024/01/01/test@example.com/xxx.eml",
"received_at": "2024-01-01 12:00:00",
"is_read": 0
}
]获取单封邮件详情
返回:
{
"id": 1,
"sender": "sender@example.com",
"to_addrs": "recipient@example.com",
"subject": "邮件主题",
"verification_code": "123456",
"content": "纯文本内容",
"html_content": "<p>HTML内容</p>",
"received_at": "2024-01-01 12:00:00",
"is_read": 1,
"download": "/api/email/1/download"
}下载原始 EML 文件
返回: message/rfc822 格式的原始邮件文件
删除单封邮件
返回:
{
"success": true,
"deleted": true,
"message": "邮件已删除"
}清空邮箱所有邮件
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
mailbox |
string | 邮箱地址(必需) |
返回:
{
"success": true,
"deletedCount": 5
}需要配置
RESEND_API_KEY环境变量
获取发件记录列表
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
from |
string | 发件人邮箱(必需) |
limit |
number | 返回数量(默认 20,最大 50) |
返回:
[
{
"id": 1,
"resend_id": "abc123",
"recipients": "to@example.com",
"subject": "邮件主题",
"created_at": "2024-01-01 12:00:00",
"status": "delivered"
}
]获取发件详情
返回:
{
"id": 1,
"resend_id": "abc123",
"from_addr": "from@example.com",
"recipients": "to@example.com",
"subject": "邮件主题",
"html_content": "<p>内容</p>",
"text_content": "内容",
"status": "delivered",
"scheduled_at": null,
"created_at": "2024-01-01 12:00:00"
}删除发件记录
返回:
{ "success": true }发送单封邮件
请求参数:
{
"from": "sender@example.com",
"fromName": "发件人名称",
"to": "recipient@example.com",
"subject": "邮件主题",
"html": "<p>HTML内容</p>",
"text": "纯文本内容",
"scheduledAt": "2024-01-02T12:00:00Z"
}返回:
{ "success": true, "id": "resend-id-xxx" }批量发送邮件
请求参数:
[
{
"from": "sender@example.com",
"to": "recipient1@example.com",
"subject": "主题1",
"html": "<p>内容1</p>"
},
{
"from": "sender@example.com",
"to": "recipient2@example.com",
"subject": "主题2",
"html": "<p>内容2</p>"
}
]返回:
{
"success": true,
"result": [
{ "id": "resend-id-1" },
{ "id": "resend-id-2" }
]
}查询发送结果(从 Resend API)
更新发送状态或定时时间
请求参数:
{
"status": "canceled",
"scheduledAt": "2024-01-03T12:00:00Z"
}取消定时发送
返回:
{ "success": true }以下接口需要
strictAdmin权限
获取用户列表
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
limit |
number | 分页大小(默认 50,最大 100) |
offset |
number | 偏移量 |
sort |
string | 排序方式:asc 或 desc(默认 desc) |
返回:
[
{
"id": 1,
"username": "testuser",
"role": "user",
"mailbox_limit": 10,
"can_send": 0,
"mailbox_count": 3,
"created_at": "2024-01-01 00:00:00"
}
]创建用户
请求参数:
{
"username": "newuser",
"password": "password123",
"role": "user",
"mailboxLimit": 10
}返回:
{
"id": 2,
"username": "newuser",
"role": "user",
"mailbox_limit": 10,
"can_send": 0,
"created_at": "2024-01-01 00:00:00"
}更新用户信息
请求参数:
{
"username": "updatedname",
"password": "newpassword",
"mailboxLimit": 20,
"can_send": 1,
"role": "admin"
}返回:
{ "success": true }删除用户
返回:
{ "success": true }获取指定用户的邮箱列表
返回:
[
{
"address": "test@example.com",
"created_at": "2024-01-01 00:00:00",
"is_pinned": 0
}
]给用户分配邮箱
请求参数:
{
"username": "testuser",
"address": "newbox@example.com"
}返回:
{ "success": true }取消用户的邮箱分配
请求参数:
{
"username": "testuser",
"address": "oldbox@example.com"
}返回:
{ "success": true }邮件接收回调(用于 Cloudflare Email Routing)
需要认证,通常由系统内部调用
所有 API 在发生错误时返回以下格式:
{
"error": "错误信息描述"
}常见 HTTP 状态码:
| 状态码 | 说明 |
|---|---|
| 400 | 请求参数错误 |
| 401 | 未认证 |
| 403 | 权限不足(演示模式限制或角色限制) |
| 404 | 资源不存在 |
| 500 | 服务器内部错误 |