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;
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,
) {
}
}

View File

@@ -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
);

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 $subject
* @param string $text
* @param string $subject
* @param string $text
* @param array<string> $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<string> $target
* @param string $subject
* @param string $html
* @param string $subject
* @param string $html
* @param array<string> $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);