From 3b3502f7634b48c31c3e7689cdcfe1f4b504b968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Tue, 4 Mar 2025 02:33:24 +0000 Subject: [PATCH] =?UTF-8?q?feat(email):=20=E6=B7=BB=E5=8A=A0=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E9=99=84=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 EmailWillSent 事件中添加 attachmentPaths 和 attachments 属性 - 在 EmailService 中添加处理附件的逻辑 - 更新 EmailWillSentListener 以支持附件发送 --- src/Events/EmailWillSent.php | 21 +++++++++++++++++---- src/Listener/EmailWillSentListener.php | 9 +++++++-- src/Service/EmailService.php | 26 ++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/Events/EmailWillSent.php b/src/Events/EmailWillSent.php index ab9b415..963b888 100644 --- a/src/Events/EmailWillSent.php +++ b/src/Events/EmailWillSent.php @@ -1,4 +1,5 @@ subject, html: $event->content, cc: $event->cc, - bcc: $event->bcc, + bcc: $event->bcc, + attachmentPaths: $event->attachmentPaths, + attachments: $event->attachments, priority: $event->priority ) : $emailService->sendText( @@ -72,7 +75,9 @@ class EmailWillSentListener implements ListenerInterface subject: $event->subject, text: $event->content, cc: $event->cc, - bcc: $event->bcc, + bcc: $event->bcc, + attachmentPaths: $event->attachmentPaths, + attachments: $event->attachments, priority: $event->priority ); diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php index 0f7cabe..651e405 100644 --- a/src/Service/EmailService.php +++ b/src/Service/EmailService.php @@ -79,7 +79,7 @@ class EmailService } /** - * @param string|array $target + * @param Address|string|array $target * @param string $subject * @param string $text * @param array $cc @@ -90,11 +90,13 @@ class EmailService * @throws TransportExceptionInterface */ public function sendText( - string|array $target, + Address|string|array $target, string $subject, string $text, array $cc = [], array $bcc = [], + array $attachmentPaths = [], + array $attachments = [], int $priority = Email::PRIORITY_NORMAL ): bool { $email = (new Email()) @@ -106,6 +108,22 @@ class EmailService ->subject($subject) ->text($text); + foreach ($attachmentPaths as $attachmentPath) { + if (is_string($attachmentPath)) { + $email = $email->attachFromPath($attachmentPath); + } else { + $email = $email->attachFromPath($attachmentPath['path'], $attachmentPath['name'] ?? null, $attachmentPath['mimeType'] ?? null); + } + } + + foreach ($attachments as $attachment) { + if (is_string($attachment)) { + $email = $email->attach($attachment); + } else { + $email = $email->attach($attachment['content'], $attachment['name'] ?? null, $attachment['mimeType'] ?? null); + } + } + $this->mailer->send($email); return true; @@ -114,7 +132,7 @@ class EmailService /** * 以 HTML 格式发送邮件 * - * @param string|array $target + * @param Address|string|array $target * @param string $subject * @param string $html * @param array $cc @@ -125,7 +143,7 @@ class EmailService * @throws TransportExceptionInterface */ public function sendHtml( - string|array $target, + Address|string|array $target, string $subject, string $html, array $cc = [],