feat(email): 添加邮件附件功能

- 在 EmailWillSent 事件中添加 attachmentPaths 和 attachments 属性
- 在 EmailService 中添加处理附件的逻辑
- 更新 EmailWillSentListener 以支持附件发送
This commit is contained in:
李东云
2025-03-04 02:33:24 +00:00
parent 768f2d79c0
commit 3b3502f763
3 changed files with 46 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* EmailWillSent.php@HDK-Core * EmailWillSent.php@HDK-Core
* *
@@ -10,6 +11,7 @@
namespace Singularity\HDK\Core\Events; namespace Singularity\HDK\Core\Events;
use Singularity\HDK\Core\Service\EmailService; use Singularity\HDK\Core\Service\EmailService;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
/** /**
@@ -25,9 +27,9 @@ class EmailWillSent
{ {
public function __construct( public function __construct(
/** /**
* @var string|string[] $target * @var Address|string|string[]|Address[] $target
*/ */
public string|array $target, public Address|string|array $target,
/** /**
* @var non-empty-string $subject * @var non-empty-string $subject
@@ -68,6 +70,17 @@ class EmailWillSent
* @var EmailService|null * @var EmailService|null
*/ */
public ?EmailService $sender = null, public ?EmailService $sender = null,
) {
} /**
* 附件路径集合、含附件路径、文件名、文件类型的数组集合
* @var string[]|array{'path': string, 'name': string, 'mimeType': string}[] $attachmentPaths
*/
public array $attachmentPaths = [],
/**
* 附件内容集合、含附件内容、文件名、文件类型的数组集合
* @var string[]|array{'content': string, 'name': string, 'mimeType': string}[] $attachments
*/
public array $attachments = [],
) {}
} }

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* EmailWillSentListener.php@HDK-Core * EmailWillSentListener.php@HDK-Core
* *
@@ -64,7 +65,9 @@ class EmailWillSentListener implements ListenerInterface
subject: $event->subject, subject: $event->subject,
html: $event->content, html: $event->content,
cc: $event->cc, cc: $event->cc,
bcc: $event->bcc, bcc: $event->bcc,
attachmentPaths: $event->attachmentPaths,
attachments: $event->attachments,
priority: $event->priority priority: $event->priority
) )
: $emailService->sendText( : $emailService->sendText(
@@ -72,7 +75,9 @@ class EmailWillSentListener implements ListenerInterface
subject: $event->subject, subject: $event->subject,
text: $event->content, text: $event->content,
cc: $event->cc, cc: $event->cc,
bcc: $event->bcc, bcc: $event->bcc,
attachmentPaths: $event->attachmentPaths,
attachments: $event->attachments,
priority: $event->priority priority: $event->priority
); );

View File

@@ -79,7 +79,7 @@ class EmailService
} }
/** /**
* @param string|array<string> $target * @param Address|string|array<string|Address> $target
* @param string $subject * @param string $subject
* @param string $text * @param string $text
* @param array<string> $cc * @param array<string> $cc
@@ -90,11 +90,13 @@ class EmailService
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
*/ */
public function sendText( public function sendText(
string|array $target, Address|string|array $target,
string $subject, string $subject,
string $text, string $text,
array $cc = [], array $cc = [],
array $bcc = [], array $bcc = [],
array $attachmentPaths = [],
array $attachments = [],
int $priority = Email::PRIORITY_NORMAL int $priority = Email::PRIORITY_NORMAL
): bool { ): bool {
$email = (new Email()) $email = (new Email())
@@ -106,6 +108,22 @@ class EmailService
->subject($subject) ->subject($subject)
->text($text); ->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); $this->mailer->send($email);
return true; return true;
@@ -114,7 +132,7 @@ class EmailService
/** /**
* 以 HTML 格式发送邮件 * 以 HTML 格式发送邮件
* *
* @param string|array<string> $target * @param Address|string|array<string|Address> $target
* @param string $subject * @param string $subject
* @param string $html * @param string $html
* @param array<string> $cc * @param array<string> $cc
@@ -125,7 +143,7 @@ class EmailService
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
*/ */
public function sendHtml( public function sendHtml(
string|array $target, Address|string|array $target,
string $subject, string $subject,
string $html, string $html,
array $cc = [], array $cc = [],