From f0470c027d207650b48dcfe579ff3526bec04893 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sat, 29 Jun 2024 21:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=82=AE=E4=BB=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Logic/Notice/External/Mail/Test.php | 26 ++--------- .../Logic/Notice/External/Mail/Verify.php | 42 ++++-------------- app/Services/Mailer.php | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/app/Services/Logic/Notice/External/Mail/Test.php b/app/Services/Logic/Notice/External/Mail/Test.php index 313a5e55..08b57e48 100644 --- a/app/Services/Logic/Notice/External/Mail/Test.php +++ b/app/Services/Logic/Notice/External/Mail/Test.php @@ -14,32 +14,14 @@ class Test extends Mailer /** * @param string $email - * @return bool + * @return bool|null */ public function handle($email) { - try { + $subject = $this->formatSubject('测试邮件'); + $content = $this->formatContent('东风快递,使命必达'); - $message = $this->manager->createMessage(); - - $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; + return $this->send($email, $subject, $content); } } diff --git a/app/Services/Logic/Notice/External/Mail/Verify.php b/app/Services/Logic/Notice/External/Mail/Verify.php index b760afaa..0f0b4ace 100644 --- a/app/Services/Logic/Notice/External/Mail/Verify.php +++ b/app/Services/Logic/Notice/External/Mail/Verify.php @@ -15,47 +15,23 @@ class Verify extends MailerService /** * @param string $email - * @return bool + * @return bool|null */ 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 = '邮件验证码'; - - $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); + return $this->send($email, $subject, $content); } } diff --git a/app/Services/Mailer.php b/app/Services/Mailer.php index 1dd9853c..9c7a2499 100644 --- a/app/Services/Mailer.php +++ b/app/Services/Mailer.php @@ -30,6 +30,49 @@ abstract class Mailer extends Service $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 */