mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-28 05:11:39 +08:00
优化邮件发送格式化
This commit is contained in:
parent
703d5cbc3d
commit
f0470c027d
26
app/Services/Logic/Notice/External/Mail/Test.php
vendored
26
app/Services/Logic/Notice/External/Mail/Test.php
vendored
@ -14,32 +14,14 @@ class Test extends Mailer
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $email
|
* @param string $email
|
||||||
* @return bool
|
* @return bool|null
|
||||||
*/
|
*/
|
||||||
public function handle($email)
|
public function handle($email)
|
||||||
{
|
{
|
||||||
try {
|
$subject = $this->formatSubject('测试邮件');
|
||||||
|
$content = $this->formatContent('东风快递,使命必达');
|
||||||
|
|
||||||
$message = $this->manager->createMessage();
|
return $this->send($email, $subject, $content);
|
||||||
|
|
||||||
$count = $message->to($email)
|
|
||||||
->subject('测试邮件')
|
|
||||||
->content('东风快递,使命必达')
|
|
||||||
->send();
|
|
||||||
|
|
||||||
$result = $count > 0;
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
|
|
||||||
$this->logger->error('Send Test Mail Exception ' . kg_json_encode([
|
|
||||||
'code' => $e->getCode(),
|
|
||||||
'message' => $e->getMessage(),
|
|
||||||
]));
|
|
||||||
|
|
||||||
$result = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,47 +15,23 @@ class Verify extends MailerService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $email
|
* @param string $email
|
||||||
* @return bool
|
* @return bool|null
|
||||||
*/
|
*/
|
||||||
public function handle($email)
|
public function handle($email)
|
||||||
{
|
{
|
||||||
try {
|
$minutes = 5;
|
||||||
|
|
||||||
$message = $this->manager->createMessage();
|
$verify = new VerifyService();
|
||||||
|
|
||||||
$verify = new VerifyService();
|
$code = $verify->getMailCode($email, 60 * $minutes);
|
||||||
|
|
||||||
$minutes = 5;
|
$subject = '邮件验证码';
|
||||||
|
$content = sprintf('验证码:%s,%s 分钟内有效,如非本人操作请忽略。', $code, $minutes);
|
||||||
|
|
||||||
$code = $verify->getMailCode($email, 60 * $minutes);
|
$subject = $this->formatSubject($subject);
|
||||||
|
$content = $this->formatSubject($content);
|
||||||
|
|
||||||
$subject = '邮件验证码';
|
return $this->send($email, $subject, $content);
|
||||||
|
|
||||||
$content = $this->formatContent($code, $minutes);
|
|
||||||
|
|
||||||
$count = $message->to($email)
|
|
||||||
->subject($subject)
|
|
||||||
->content($content)
|
|
||||||
->send();
|
|
||||||
|
|
||||||
$result = $count > 0;
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
|
|
||||||
$this->logger->error('Send Verify Mail Exception ' . kg_json_encode([
|
|
||||||
'code' => $e->getCode(),
|
|
||||||
'message' => $e->getMessage(),
|
|
||||||
]));
|
|
||||||
|
|
||||||
$result = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function formatContent($code, $minutes)
|
|
||||||
{
|
|
||||||
return sprintf('验证码:%s,%s 分钟内有效,如非本人操作请忽略。', $code, $minutes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,49 @@ abstract class Mailer extends Service
|
|||||||
$this->logger = $this->getLogger('mail');
|
$this->logger = $this->getLogger('mail');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function send($email, $subject, $content, $attachment = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
$message = $this->manager->createMessage();
|
||||||
|
|
||||||
|
$message->to($email);
|
||||||
|
$message->subject($subject);
|
||||||
|
$message->content($content);
|
||||||
|
|
||||||
|
if ($attachment) {
|
||||||
|
$message->attachment($attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = $message->send();
|
||||||
|
|
||||||
|
$result = $count > 0;
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
|
$this->logger->error('Send Mail Exception ' . kg_json_encode([
|
||||||
|
'code' => $e->getCode(),
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
]));
|
||||||
|
|
||||||
|
$result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function formatSubject($subject)
|
||||||
|
{
|
||||||
|
$site = $this->getSettings('site');
|
||||||
|
|
||||||
|
return sprintf('【%s】%s', $site['title'], $subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function formatContent($content)
|
||||||
|
{
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 Manager
|
* 获取 Manager
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user