Files
Fuxsto-V4/Main/System/try.php
2025-10-18 10:19:37 +08:00

75 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 邮件接口调用函数
* @param string $收件人 收件人邮箱地址
* @param string $主题 邮件主题
* @param string $内容 邮件正文内容
* @param string $接口地址 默认接口地址
* @return int|string 成功返回200失败返回错误信息
*/
function 发送邮件接口($收件人, $主题, $内容, $接口地址 = 'https://hv3.fuxsto.cn/user/OpenEmailApi.php')
{
// 参数有效性验证
if (!filter_var($收件人, FILTER_VALIDATE_EMAIL)) {
return "收件人邮箱格式无效";
}
// 准备请求参数(自动编码处理)
$请求数据 = http_build_query([
'to' => $收件人,
'subject' => $主题,
'msg' => $内容
]);
// 初始化cURL
$ch = curl_init();
// 配置cURL选项
curl_setopt_array($ch, [
CURLOPT_URL => $接口地址,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true, // 验证SSL证书
CURLOPT_TIMEOUT => 15, // 15秒超时
CURLOPT_CONNECTTIMEOUT => 5, // 5秒连接超时
CURLOPT_POSTFIELDS => $请求数据,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
]
]);
// 执行请求
$响应 = curl_exec($ch);
// 错误处理
if (curl_errno($ch)) {
$错误信息 = '网络请求失败: ' . curl_error($ch);
curl_close($ch);
return $错误信息;
}
// 获取HTTP状态码
$状态码 = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 处理响应
if ($状态码 !== 200) {
return "接口响应异常 (HTTP {$状态码})";
}
// 验证响应内容
return ($响应 === '200') ? 200 : trim($响应);
}
$结果 = 发送邮件接口(
'3220257676@qq.com',
'测试邮件主题',
file_get_contents("./Res/ExpiredProductsCleaner.html")
);
if ($结果 === 200) {
echo '邮件发送成功';
} else {
echo '发送失败: ' . htmlspecialchars($结果);
}