233 lines
8.4 KiB
PHP
233 lines
8.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller\System;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Session;
|
|
use think\response\Json;
|
|
use think\db\exception\DbException;
|
|
use app\model\System\SystemEmail;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception as MailException;
|
|
|
|
class EmailController extends BaseController
|
|
{
|
|
/**
|
|
* 获取邮箱信息
|
|
* @return Json
|
|
*/
|
|
public function getEmailInfo()
|
|
{
|
|
try {
|
|
$email = SystemEmail::order('id', 'asc')
|
|
->field('id, from_address, from_name, host, port, password, encryption, timeout, create_time, update_time')
|
|
->select()
|
|
->toArray();
|
|
// 记录操作日志
|
|
$this->logSuccess('邮箱管理', '获取邮箱信息', ['data' => $email]);
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $email
|
|
]);
|
|
} catch (DbException $e) {
|
|
// 记录失败日志
|
|
$this->logFail('邮箱管理', '获取邮箱信息', $e->getMessage());
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '获取失败:' . $e->getMessage(),
|
|
'data' => []
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑邮箱信息
|
|
* @return Json
|
|
*/
|
|
public function editEmailInfo()
|
|
{
|
|
try {
|
|
// 获取原始请求参数(驼峰命名)
|
|
$raw = $this->request->post();
|
|
|
|
// 手动映射为下划线命名,便于与数据库字段、验证规则对应
|
|
$data = [
|
|
'from_address' => $raw['fromAddress'] ?? '',
|
|
'from_name' => $raw['fromName'] ?? '',
|
|
'host' => $raw['host'] ?? '',
|
|
'port' => $raw['port'] ?? '',
|
|
'password' => $raw['password'] ?? '',
|
|
'encryption' => $raw['encryption'] ?? '',
|
|
'timeout' => $raw['timeout'] ?? '',
|
|
];
|
|
|
|
// 验证参数
|
|
$this->validate($data, [
|
|
'from_address|发件人邮箱' => 'require|max:255',
|
|
'from_name|发件人名称' => 'require|max:255',
|
|
'host|SMTP主机' => 'require|max:255',
|
|
'port|SMTP端口' => 'require|integer',
|
|
'password|密码' => 'require|max:255',
|
|
'encryption|加密方式' => 'require|max:255',
|
|
'timeout|超时时间' => 'require|integer',
|
|
]);
|
|
|
|
// 先查一条配置(本表设计为单条配置)
|
|
$email = SystemEmail::order('id', 'asc')->find();
|
|
|
|
// 公共字段
|
|
$saveData = [
|
|
'from_address' => $data['from_address'],
|
|
'from_name' => $data['from_name'],
|
|
'host' => $data['host'],
|
|
'port' => $data['port'],
|
|
'password' => $data['password'],
|
|
'encryption' => $data['encryption'],
|
|
'timeout' => $data['timeout'],
|
|
];
|
|
|
|
if ($email) {
|
|
// 已有一条记录,按主键更新
|
|
$saveData['update_time'] = date('Y-m-d H:i:s');
|
|
SystemEmail::update($saveData, ['id' => $email['id']]);
|
|
} else {
|
|
// 没有记录,插入新记录
|
|
$saveData['create_time'] = date('Y-m-d H:i:s');
|
|
$saveData['update_time'] = $saveData['create_time'];
|
|
$model = new SystemEmail();
|
|
$model->save($saveData);
|
|
}
|
|
|
|
// 获取最新的邮箱信息
|
|
$updatedEmail = SystemEmail::order('id', 'asc')->find();
|
|
|
|
// 记录操作日志
|
|
$this->logSuccess('邮箱管理', '更新邮箱信息', ['data' => $updatedEmail]);
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '更新成功',
|
|
'data' => $updatedEmail
|
|
]);
|
|
} catch (ValidateException $e) {
|
|
// 记录失败日志
|
|
$this->logFail('邮箱管理', '更新邮箱信息', $e->getMessage());
|
|
return json([
|
|
'code' => 400,
|
|
'msg' => $e->getError()
|
|
]);
|
|
} catch (DbException $e) {
|
|
// 记录失败日志
|
|
$this->logFail('邮箱管理', '更新邮箱信息', $e->getMessage());
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '更新失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
/**
|
|
* 发送测试邮件
|
|
* @return Json
|
|
*/
|
|
public function sendTestEmail()
|
|
{
|
|
try {
|
|
// 获取前端传来的表单数据(驼峰命名)
|
|
$raw = $this->request->post();
|
|
|
|
// 映射为下划线命名,便于验证和使用
|
|
$data = [
|
|
'from_address' => $raw['fromAddress'] ?? '',
|
|
'from_name' => $raw['fromName'] ?? '',
|
|
'host' => $raw['host'] ?? '',
|
|
'port' => $raw['port'] ?? '',
|
|
'password' => $raw['password'] ?? '',
|
|
'encryption' => $raw['encryption'] ?? '',
|
|
'timeout' => $raw['timeout'] ?? 30,
|
|
'test_email' => $raw['testEmail'] ?? '',
|
|
];
|
|
|
|
// 参数验证
|
|
$this->validate($data, [
|
|
'from_address|发件人邮箱' => 'require|email|max:255',
|
|
'from_name|发件人名称' => 'max:255',
|
|
'host|SMTP主机' => 'require|max:255',
|
|
'port|SMTP端口' => 'require|integer',
|
|
'password|密码' => 'require|max:255',
|
|
'encryption|加密方式' => 'require|max:10',
|
|
'timeout|超时时间' => 'integer',
|
|
'test_email|测试收件邮箱' => 'require|email|max:255',
|
|
]);
|
|
|
|
// 使用 PHPMailer 发送测试邮件
|
|
$mail = new PHPMailer(true);
|
|
|
|
// 服务器设置
|
|
$mail->CharSet = 'UTF-8';
|
|
$mail->isSMTP();
|
|
$mail->Host = $data['host'];
|
|
$mail->SMTPAuth = true;
|
|
// 发件人邮箱即为登录用户名
|
|
$mail->Username = $data['from_address'];
|
|
$mail->Password = $data['password'];
|
|
$mail->Port = (int)$data['port'];
|
|
|
|
// 加密方式
|
|
if (in_array(strtolower((string)$data['encryption']), ['ssl', 'tls'], true)) {
|
|
$mail->SMTPSecure = strtolower((string)$data['encryption']);
|
|
}
|
|
|
|
// 超时时间(秒)
|
|
if (!empty($data['timeout'])) {
|
|
$mail->Timeout = (int)$data['timeout'];
|
|
}
|
|
|
|
// 收发件配置
|
|
$fromName = $data['from_name'] ?: $data['from_address'];
|
|
$mail->setFrom($data['from_address'], $fromName);
|
|
$mail->addAddress($data['test_email']);
|
|
|
|
// 内容
|
|
$mail->isHTML(false);
|
|
$mail->Subject = '测试邮件 - 网站邮箱配置';
|
|
$mail->Body = '这是一封测试邮件,用于验证网站邮箱配置是否正确。发送时间:' . date('Y-m-d H:i:s');
|
|
|
|
// 发送
|
|
$mail->send();
|
|
|
|
// 记录操作日志
|
|
$this->logSuccess('邮箱管理', '发送测试邮件', [
|
|
'to' => $data['test_email'],
|
|
'host' => $data['host'],
|
|
]);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '测试邮件发送成功',
|
|
]);
|
|
} catch (ValidateException $e) {
|
|
$this->logFail('邮箱管理', '发送测试邮件', $e->getMessage());
|
|
return json([
|
|
'code' => 400,
|
|
'msg' => $e->getError()
|
|
]);
|
|
} catch (MailException $e) {
|
|
$this->logFail('邮箱管理', '发送测试邮件', $e->getMessage());
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '发送失败:' . $e->getMessage()
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->logFail('邮箱管理', '发送测试邮件', $e->getMessage());
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '发送失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
}
|