feat(email): 对发送邮件增加密送,增加单独定制发件人的方法

Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
This commit is contained in:
李东云
2024-09-12 19:47:14 +08:00
parent 0283c7286a
commit b6dd7dd4ac
3 changed files with 45 additions and 7 deletions

View File

@@ -9,6 +9,7 @@
namespace Singularity\HDK\Core\Events; namespace Singularity\HDK\Core\Events;
use Singularity\HDK\Core\Service\EmailService;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
/** /**
@@ -39,10 +40,19 @@ class EmailWillSent
public string $content, public string $content,
/** /**
* 抄送
*
* @var string[] $cc * @var string[] $cc
*/ */
public array $cc = [], public array $cc = [],
/**
* 密送
*
* @var string[] $bcc
*/
public array $bcc = [],
/** /**
* @var 'text'|'html' $type * @var 'text'|'html' $type
*/ */
@@ -52,7 +62,12 @@ class EmailWillSent
* @var int * @var int
* @see Email::priority() * @see Email::priority()
*/ */
public int $priority = Email::PRIORITY_NORMAL public int $priority = Email::PRIORITY_NORMAL,
/**
* @var EmailService|null
*/
public ?EmailService $sender = null,
) { ) {
} }
} }

View File

@@ -55,7 +55,8 @@ class EmailWillSentListener implements ListenerInterface
public function process(object $event): void public function process(object $event): void
{ {
$stdoutLogger = $this->container->get(StdoutLoggerInterface::class); $stdoutLogger = $this->container->get(StdoutLoggerInterface::class);
$emailService = $this->container->get(EmailService::class); // $emailService = $this->container->get(EmailService::class);
$emailService = $event->sender ?? EmailService::make();
try { try {
$event->type === 'html' $event->type === 'html'
? $emailService->sendHtml( ? $emailService->sendHtml(
@@ -63,6 +64,7 @@ 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,
priority: $event->priority priority: $event->priority
) )
: $emailService->sendText( : $emailService->sendText(
@@ -70,6 +72,7 @@ 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,
priority: $event->priority priority: $event->priority
); );

View File

@@ -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<string> $target * @param string|array<string> $target
* @param string $subject * @param string $subject
* @param string $text * @param string $text
* @param array<string> $cc * @param array<string> $cc
* @param array $bcc
* @param int $priority
* *
* @return bool * @return bool
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
@@ -80,12 +94,14 @@ class EmailService
string $subject, string $subject,
string $text, string $text,
array $cc = [], array $cc = [],
array $bcc = [],
int $priority = Email::PRIORITY_NORMAL int $priority = Email::PRIORITY_NORMAL
): bool { ): bool {
$email = (new Email()) $email = (new Email())
->from(Address::create($this->from)) ->from(Address::create($this->from))
->to(...(is_array($target) ? $target : [$target])) ->to(...(is_array($target) ? $target : [$target]))
->cc(...$cc) ->cc(...$cc)
->bcc(...$bcc)
->priority($priority) ->priority($priority)
->subject($subject) ->subject($subject)
->text($text); ->text($text);
@@ -99,10 +115,12 @@ class EmailService
* 以 HTML 格式发送邮件 * 以 HTML 格式发送邮件
* *
* @param string|array<string> $target * @param string|array<string> $target
* @param string $subject * @param string $subject
* @param string $html * @param string $html
* @param array<string> $cc * @param array<string> $cc
* @param int $priority * @param array $bcc
* @param int $priority
*
* @return bool * @return bool
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
*/ */
@@ -111,12 +129,14 @@ class EmailService
string $subject, string $subject,
string $html, string $html,
array $cc = [], array $cc = [],
array $bcc = [],
int $priority = Email::PRIORITY_NORMAL int $priority = Email::PRIORITY_NORMAL
): bool { ): bool {
$email = (new Email()) $email = (new Email())
->from(Address::create($this->from)) ->from(Address::create($this->from))
->to(...(is_array($target) ? $target : [$target])) ->to(...(is_array($target) ? $target : [$target]))
->cc(...$cc) ->cc(...$cc)
->bcc(...$bcc)
->priority($priority) ->priority($priority)
->subject($subject) ->subject($subject)
->html($html); ->html($html);