初始化-安装依赖
This commit is contained in:
100
server/plugin/saiadmin/service/EmailService.php
Normal file
100
server/plugin/saiadmin/service/EmailService.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\service;
|
||||
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use plugin\saiadmin\app\logic\system\SystemConfigLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Arr;
|
||||
|
||||
/**
|
||||
* 邮件服务类
|
||||
*/
|
||||
class EmailService
|
||||
{
|
||||
/**
|
||||
* 读取配置
|
||||
* @return array
|
||||
*/
|
||||
public static function getConfig(): array
|
||||
{
|
||||
$logic = new SystemConfigLogic();
|
||||
$config = $logic->getGroup('email_config');
|
||||
if (!$config) {
|
||||
throw new ApiException('未设置邮件配置');
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Mailer
|
||||
* @return PHPMailer
|
||||
*/
|
||||
public static function getMailer(): PHPMailer
|
||||
{
|
||||
if (!class_exists(PHPMailer::class)) {
|
||||
throw new ApiException('请执行 composer require phpmailer/phpmailer 并重启');
|
||||
}
|
||||
$config = static::getConfig();
|
||||
$mailer = new PHPMailer();
|
||||
$mailer->SMTPDebug = intval(Arr::getConfigValue($config,'SMTPDebug'));
|
||||
$mailer->isSMTP();
|
||||
$mailer->Host = Arr::getConfigValue($config,'Host');
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->CharSet = Arr::getConfigValue($config,'CharSet');
|
||||
$mailer->Username = Arr::getConfigValue($config,'Username');
|
||||
$mailer->Password = Arr::getConfigValue($config,'Password');
|
||||
$mailer->SMTPSecure = Arr::getConfigValue($config,'SMTPSecure');
|
||||
$mailer->Port = Arr::getConfigValue($config,'Port');
|
||||
return $mailer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
* @param $from
|
||||
* @param $to
|
||||
* @param $subject
|
||||
* @param $content
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function send($from, $to, $subject, $content): string
|
||||
{
|
||||
$mailer = static::getMailer();
|
||||
call_user_func_array([$mailer, 'setFrom'], (array)$from);
|
||||
call_user_func_array([$mailer, 'addAddress'], (array)$to);
|
||||
$mailer->Subject = $subject;
|
||||
$mailer->isHTML(true);
|
||||
$mailer->Body = $content;
|
||||
$mailer->send();
|
||||
return $mailer->ErrorInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照模版发送
|
||||
* @param string|array $to
|
||||
* @param $subject
|
||||
* @param $content
|
||||
* @param array $templateData
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function sendByTemplate($to, $subject, $content, array $templateData = []): string
|
||||
{
|
||||
if ($templateData) {
|
||||
$search = [];
|
||||
foreach ($templateData as $key => $value) {
|
||||
$search[] = '{' . $key . '}';
|
||||
}
|
||||
$content = str_replace($search, array_values($templateData), $content);
|
||||
}
|
||||
$config = static::getConfig();
|
||||
return static::send([Arr::getConfigValue($config,'From'), Arr::getConfigValue($config,'FromName')], $to, $subject, $content);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user