优化文本消息

main
gzydong 2021-02-04 23:36:14 +08:00
parent 0b860d3fad
commit e5359a27d3
4 changed files with 144 additions and 5 deletions

View File

@ -0,0 +1,83 @@
<?php
namespace App\Service;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/**
* Class MailerService
* @package App\Service
*/
class MailerService
{
/**
* 消息队列
*
* @var bool
*/
public $queueSwitch = false;
/**
* 发送邮件
*
* @param string $email 邮箱
* @param string $subject 标题
* @param string $template 对应邮件模板
* @return bool
*/
public function send($email, $subject, $template)
{
if ($this->queueSwitch) {
}
return $this->realSend($email, $subject, $template);
}
/**
* 发送邮件
*
* @param string $email 邮箱
* @param string $subject 标题
* @param string $template 对应邮件模板
* @return bool
*/
public function realSend($email, $subject, $template)
{
try {
return $this->mail($email, $subject, $template);
} catch (Exception $e) {
return false;
}
}
/**
* 推送邮件
*
* @param string $address 收件人
* @param string $subject 邮件标题
* @param string $view 邮件内容
* @return bool
* @throws Exception
*/
private function mail(string $address, string $subject, string $view): bool
{
$config = config('mail');
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // 关闭SMTP调试功能
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = 'ssl'; // 使用安全协议
$mail->Host = $config['host'];
$mail->Port = $config['port'];
$mail->Username = $config['username'];
$mail->Password = $config['password'];
$mail->SetFrom($config['from'], $config['name']);
$mail->Subject = $subject;
$mail->MsgHTML($view);
$mail->AddAddress($address); // 收件人
return $mail->Send();
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Service;
/**
* 短信服务
* Class SmsService
* @package App\Service
*/
class SmsService
{
}

View File

@ -180,11 +180,6 @@ class TalkService extends BaseService
$rows[$k]['invite'] = [];
switch ($row['msg_type']) {
case 1://1:文本消息
if (!empty($rows[$k]['content'])) {
$rows[$k]['content'] = replace_url_link($row['content']);
}
break;
case 2://2:文件消息
$rows[$k]['file'] = $files[$row['id']] ?? [];
if ($rows[$k]['file']) {

View File

@ -0,0 +1,48 @@
<?php
namespace App\Support;
/**
* 邮件视图模板
* Class MailerViewTemplate
* @package App\Support
*/
class MailerTemplate
{
/**
* 获取模板信息
*
* @param string $engine 模板引擎
* @param string $template 模板名称
* @param array $params 模板参数
* @return string
*/
public function view(string $engine, string $template, $params = []): string
{
return container()->get($engine)->render($template, $params, config('view.config', []));
}
/**
* 邮件验证码模板
*
* @param string $sms_code 验证码
* @param array $params 模板参数
* @return string
*/
public function emailCode(string $sms_code, array $params = [])
{
return $this->view(config('view.engine'), 'emails.verify-code', [
'service_name' => $params['service_name'] ?? "邮箱绑定",
'sms_code' => $sms_code,
'domain' => $params['web_url'] ?? config('domain.web_url')
]);
}
/**
* 注册成功通知
*/
public function register()
{
}
}