From b6dd7dd4ace2623a4631d22fc4e6394e9d2ddda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Thu, 12 Sep 2024 19:47:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(email):=20=E5=AF=B9=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=A2=9E=E5=8A=A0=E5=AF=86=E9=80=81=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E7=8B=AC=E5=AE=9A=E5=88=B6=E5=8F=91?= =?UTF-8?q?=E4=BB=B6=E4=BA=BA=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李东云 --- src/Events/EmailWillSent.php | 17 ++++++++++++++- src/Listener/EmailWillSentListener.php | 5 ++++- src/Service/EmailService.php | 30 +++++++++++++++++++++----- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/Events/EmailWillSent.php b/src/Events/EmailWillSent.php index bd24baf..ab9b415 100644 --- a/src/Events/EmailWillSent.php +++ b/src/Events/EmailWillSent.php @@ -9,6 +9,7 @@ namespace Singularity\HDK\Core\Events; +use Singularity\HDK\Core\Service\EmailService; use Symfony\Component\Mime\Email; /** @@ -39,10 +40,19 @@ class EmailWillSent public string $content, /** + * 抄送 + * * @var string[] $cc */ public array $cc = [], + /** + * 密送 + * + * @var string[] $bcc + */ + public array $bcc = [], + /** * @var 'text'|'html' $type */ @@ -52,7 +62,12 @@ class EmailWillSent * @var int * @see Email::priority() */ - public int $priority = Email::PRIORITY_NORMAL + public int $priority = Email::PRIORITY_NORMAL, + + /** + * @var EmailService|null + */ + public ?EmailService $sender = null, ) { } } diff --git a/src/Listener/EmailWillSentListener.php b/src/Listener/EmailWillSentListener.php index 5317414..f271a97 100644 --- a/src/Listener/EmailWillSentListener.php +++ b/src/Listener/EmailWillSentListener.php @@ -55,7 +55,8 @@ class EmailWillSentListener implements ListenerInterface public function process(object $event): void { $stdoutLogger = $this->container->get(StdoutLoggerInterface::class); - $emailService = $this->container->get(EmailService::class); + // $emailService = $this->container->get(EmailService::class); + $emailService = $event->sender ?? EmailService::make(); try { $event->type === 'html' ? $emailService->sendHtml( @@ -63,6 +64,7 @@ class EmailWillSentListener implements ListenerInterface subject: $event->subject, html: $event->content, cc: $event->cc, + bcc: $event->bcc, priority: $event->priority ) : $emailService->sendText( @@ -70,6 +72,7 @@ class EmailWillSentListener implements ListenerInterface subject: $event->subject, text: $event->content, cc: $event->cc, + bcc: $event->bcc, priority: $event->priority ); diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php index e9a3f7d..d7bf0fc 100644 --- a/src/Service/EmailService.php +++ b/src/Service/EmailService.php @@ -45,6 +45,18 @@ class EmailService ); } + public static function make( + ?string $dsn = null, + ?string $mailSenderName = null, + ?string $mailSender = null, + ): static { + return new static( + dsn: $dsn, + mailSenderName: $mailSenderName, + mailSender: $mailSender, + ); + } + /** * 发送邮件 * @@ -68,9 +80,11 @@ class EmailService /** * @param string|array $target - * @param string $subject - * @param string $text + * @param string $subject + * @param string $text * @param array $cc + * @param array $bcc + * @param int $priority * * @return bool * @throws TransportExceptionInterface @@ -80,12 +94,14 @@ class EmailService string $subject, string $text, array $cc = [], + array $bcc = [], int $priority = Email::PRIORITY_NORMAL ): bool { $email = (new Email()) ->from(Address::create($this->from)) ->to(...(is_array($target) ? $target : [$target])) ->cc(...$cc) + ->bcc(...$bcc) ->priority($priority) ->subject($subject) ->text($text); @@ -99,10 +115,12 @@ class EmailService * 以 HTML 格式发送邮件 * * @param string|array $target - * @param string $subject - * @param string $html + * @param string $subject + * @param string $html * @param array $cc - * @param int $priority + * @param array $bcc + * @param int $priority + * * @return bool * @throws TransportExceptionInterface */ @@ -111,12 +129,14 @@ class EmailService string $subject, string $html, array $cc = [], + array $bcc = [], int $priority = Email::PRIORITY_NORMAL ): bool { $email = (new Email()) ->from(Address::create($this->from)) ->to(...(is_array($target) ? $target : [$target])) ->cc(...$cc) + ->bcc(...$bcc) ->priority($priority) ->subject($subject) ->html($html);