fuc-new/src/Services/MailMust_reng.php
2026-05-08 16:08:40 +08:00

74 lines
1.8 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Config;
use App\Models\MailMust as ModelMailMust;
use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Smarty\Smarty;
use App\Services\MailMust;
use App\Services\Mail\Resend;
use App\Models\MailLog as ModelMailLog;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
final class MailMust
{
public static function getClient()
{
$oMailMust = ModelMailMust::where("status", 1)->orderBy("sort")->first();
if (!$oMailMust) {
return new NullMail();
}
return match ($oMailMust->client) {
'resend' => new Resend(),
'smtp' => new Smtp(),
default => new NullMail(),
};
}
public static function send($to, $subject, $template, $array = [], $scene = ''): void
{
//
$sGroupUse = Config::_hit("mailMustMap_clientGroup:".$scene);
if ($sGroupUse == "must") {
$oClient = self::getClient();
} else {
$oClient = Mail::getClient();
}
$oMailLog = new ModelMailLog();
$oMailLog->to = $to;
$oMailLog->title = $subject;
$oMailLog->scene = $scene;
$oMailLog->client = $oClient->getClientCode();
$oMailLog->date = date("Y-m-d H:i:s");
$oMailLog->save();
$body = Mail::genHtml($template, $array);
try {
$oClient->send($to, $subject, $body);
} catch (\Exception $e) {
TeleSlave::warn()->send("有邮件发送失败({$oMailLog->id}):".$e->getMessage());
throw $e;
}
}
public static function sendVcode($to, $subject, $template, $array = [], $scene = 'vcode')
{
self::send($to, $subject, $template, $array, $scene);
}
}