forked from easychen/wecomchan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail-api.php
More file actions
144 lines (127 loc) · 3.8 KB
/
mail-api.php
File metadata and controls
144 lines (127 loc) · 3.8 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
// -----------------------------
// 环境变量配置
// -----------------------------
// 从环境变量获取域名,如果没有则使用默认值
$baseDomain = getenv('BASE_DOMAIN') ?: '自行填写域名';
// 自定义接收路径的密钥(建议设置为随机字符串)
$receiveSecret = getenv('RECEIVE_SECRET') ?: '自行填写密钥,会转换为路径';
// -----------------------------
// Redis 连接(原生扩展)
// -----------------------------
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
// 如果你有密码
// $redis->auth('password');
// -----------------------------
// 路由解析
// -----------------------------
$uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$method = $_SERVER['REQUEST_METHOD'];
/*
|--------------------------------------------------------------------------
| /receive/{secret} —— 接收 HTML,存 Redis,返回完整 URL
|--------------------------------------------------------------------------
*/
if (preg_match('#^receive/([a-zA-Z0-9_-]+)$#', $uri, $matches) && $method === 'POST') {
$providedSecret = $matches[1];
// 验证密钥
if ($providedSecret !== $receiveSecret) {
http_response_code(403);
header('Content-Type: application/json');
echo json_encode(['error' => 'forbidden']);
exit;
}
// 获取 POST 数据
$rawInput = file_get_contents('php://input');
$data = json_decode($rawInput, true);
// 检查 JSON 解析错误
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['error' => 'invalid json: ' . json_last_error_msg()]);
exit;
}
$html = $data['html'] ?? null;
if (!$html) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['error' => 'no html']);
exit;
}
// 生成随机 key
$key = bin2hex(random_bytes(16));
// 存储 7 天(秒)
$redisResult = $redis->setex("mail:$key", 7 * 86400, $html);
// 检查 Redis 存储是否成功
if (!$redisResult) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(['error' => 'redis storage failed']);
exit;
}
// 构建完整 URL
$viewUrl = "https://{$baseDomain}/view/{$key}";
http_response_code(200);
header('Content-Type: application/json');
echo json_encode([
'url' => $viewUrl,
'expires_in' => 7 * 86400
]);
exit;
}
/*
|--------------------------------------------------------------------------
| /view/{key} —— 渲染邮件(允许 iframe)
|--------------------------------------------------------------------------
*/
if (preg_match('#^view/([a-f0-9]{32})$#', $uri, $m)) {
$key = $m[1];
$html = $redis->get("mail:$key");
if ($html === false) {
http_response_code(404);
echo 'Mail expired or not found';
exit;
}
header('Content-Type: text/html; charset=UTF-8');
header('X-Robots-Tag: noindex, nofollow');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Email Preview</title>
<base target="_blank">
<style>
body {
margin: 0;
background: #f1f3f4;
font-family: Roboto, Arial, sans-serif;
}
.mail-wrapper {
max-width: 900px;
margin: 24px auto;
background: #ffffff;
padding: 24px;
border-radius: 8px;
}
</style>
</head>
<body>
<div class="mail-wrapper">
<?= $html ?>
</div>
</body>
</html>
<?php
exit;
}
/*
|--------------------------------------------------------------------------
| 404
|--------------------------------------------------------------------------
*/
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'not found']);
?>