-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-verification-code.php
More file actions
130 lines (106 loc) · 3.91 KB
/
Copy pathsend-verification-code.php
File metadata and controls
130 lines (106 loc) · 3.91 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
<?php
require_once __DIR__ . '/config/memcache.php';
require_once __DIR__ . '/config/smtp.php';
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'success' => false,
'message' => 'Method Not Allowed'
]);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$email = trim($input['email'] ?? '');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Invalid email']);
exit;
}
$existingCode = getVerificationCode($email);
if ($existingCode !== false) {
http_response_code(429);
echo json_encode(['success' => false, 'message' => 'Verification code already sent, please wait']);
exit;
}
$code = str_pad((string)random_int(0, 999999), 6, '0', STR_PAD_LEFT);
if (!storeVerificationCode($email, $code)) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Failed to store verification code']);
exit;
}
function sendSMTPMail($to, $subject, $message, $smtp) {
$host = $smtp['host'];
$port = $smtp['port'];
$from = $smtp['from']['email'];
$fromName = $smtp['from']['name'];
$socket = @fsockopen($host, $port, $errno, $errstr, 10);
if (!$socket) {
throw new Exception("无法连接到 SMTP 服务器: $errstr ($errno)");
}
function readSMTPResponse($socket) {
$response = '';
while ($line = fgets($socket, 515)) {
$response .= $line;
if (strlen($line) >= 4 && $line[3] == ' ') {
break;
}
}
return $response;
}
$response = readSMTPResponse($socket);
if (substr($response, 0, 3) != '220') {
fclose($socket);
throw new Exception("SMTP 服务器未响应: $response");
}
fputs($socket, "EHLO " . gethostname() . "\r\n");
$response = readSMTPResponse($socket);
fputs($socket, "MAIL FROM: <$from>\r\n");
$response = readSMTPResponse($socket);
if (substr($response, 0, 3) != '250') {
fclose($socket);
throw new Exception("MAIL FROM 失败: $response");
}
fputs($socket, "RCPT TO: <$to>\r\n");
$response = readSMTPResponse($socket);
if (substr($response, 0, 3) != '250') {
fclose($socket);
throw new Exception("RCPT TO 失败: $response");
}
fputs($socket, "DATA\r\n");
$response = readSMTPResponse($socket);
if (substr($response, 0, 3) != '354') {
fclose($socket);
throw new Exception("DATA 失败: $response");
}
$headers = "From: =?UTF-8?B?" . base64_encode($fromName) . "?= <$from>\r\n";
$headers .= "To: <$to>\r\n";
$headers .= "Subject: =?UTF-8?B?" . base64_encode($subject) . "?=\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$body = chunk_split(base64_encode($message));
fputs($socket, $headers . "\r\n" . $body . "\r\n.\r\n");
$response = readSMTPResponse($socket);
if (substr($response, 0, 3) != '250') {
fclose($socket);
throw new Exception("邮件发送失败: $response");
}
fputs($socket, "QUIT\r\n");
fclose($socket);
return true;
}
try {
$to = $email;
$subject = 'HRPAuth - 邮箱验证码';
$message = "您的验证码是: {$code}\n\n验证码有效期为10分钟,请尽快完成验证。\n\n如果您没有请求此验证码,请忽略此邮件。";
sendSMTPMail($to, $subject, $message, $SMTP);
echo json_encode([
'success' => true,
'message' => 'Verification code sent successfully'
]);
} catch (Exception $e) {
deleteVerificationCode($email);
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}