-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-test-email.php
More file actions
134 lines (110 loc) · 3.75 KB
/
Copy pathsend-test-email.php
File metadata and controls
134 lines (110 loc) · 3.75 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
<?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);
$to = trim($input['to'] ?? '');
$subject = trim($input['subject'] ?? '测试邮件');
$message = trim($input['message'] ?? '');
if (empty($to)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => '收件人邮箱不能为空']);
exit;
}
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => '收件人邮箱格式不正确']);
exit;
}
if (empty($subject)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => '邮件主题不能为空']);
exit;
}
if (empty($message)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => '邮件内容不能为空']);
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 {
sendSMTPMail($to, $subject, $message, $SMTP);
echo json_encode([
'success' => true,
'message' => '邮件发送成功',
'data' => [
'to' => $to,
'subject' => $subject
]
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}