mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
- 在 EmailWillSent 事件中添加 attachmentPaths 和 attachments 属性 - 在 EmailService 中添加处理附件的逻辑 - 更新 EmailWillSentListener 以支持附件发送
185 lines
5.2 KiB
PHP
185 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* This file is part of LuxAccountX.
|
|
*
|
|
* @link http://124.126.16.154:8888/WebService/LuxAccountX
|
|
* @document https://lux-software.yuque.com/htnx76/vcm2oc
|
|
* @contact dongyun.li@luxcreo.ai
|
|
*/
|
|
|
|
namespace Singularity\HDK\Core\Service;
|
|
|
|
use JetBrains\PhpStorm\Deprecated;
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
use Symfony\Component\Mailer\Mailer;
|
|
use Symfony\Component\Mailer\Transport;
|
|
use Symfony\Component\Mime\Address;
|
|
use Symfony\Component\Mime\Email;
|
|
|
|
use function Hyperf\Config\config;
|
|
|
|
/**
|
|
* 邮箱验证码
|
|
*/
|
|
class EmailService
|
|
{
|
|
private Mailer $mailer;
|
|
|
|
private string $from;
|
|
|
|
public function __construct(
|
|
?string $dsn = null,
|
|
?string $mailSenderName = null,
|
|
?string $mailSender = null
|
|
) {
|
|
$dsn ??= config('common.third_party.email.dsn');
|
|
$transport = Transport::fromDsn($dsn);
|
|
$this->mailer = new Mailer($transport);
|
|
|
|
$this->from = sprintf(
|
|
"%s <%s>",
|
|
$mailSenderName ?? config('common.third_party.email.mailer_sender_name'),
|
|
$mailSender ?? config('common.third_party.email.mailer_sender')
|
|
);
|
|
}
|
|
|
|
public static function make(
|
|
?string $dsn = null,
|
|
?string $mailSenderName = null,
|
|
?string $mailSender = null,
|
|
): static {
|
|
return new static(
|
|
dsn: $dsn,
|
|
mailSenderName: $mailSenderName,
|
|
mailSender: $mailSender,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 发送邮件
|
|
*
|
|
* @param string $target
|
|
* @param string $subject
|
|
* @param string $text
|
|
*
|
|
* @return bool
|
|
* @throws TransportExceptionInterface
|
|
* @see EmailService::sendText
|
|
* @see EmailService::sendHtml
|
|
*/
|
|
#[Deprecated]
|
|
public function sendCode(
|
|
string $target,
|
|
string $subject,
|
|
string $text
|
|
): bool {
|
|
return $this->sendText($target, $subject, $text);
|
|
}
|
|
|
|
/**
|
|
* @param Address|string|array<string|Address> $target
|
|
* @param string $subject
|
|
* @param string $text
|
|
* @param array<string> $cc
|
|
* @param array $bcc
|
|
* @param int $priority
|
|
*
|
|
* @return bool
|
|
* @throws TransportExceptionInterface
|
|
*/
|
|
public function sendText(
|
|
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())
|
|
->from(Address::create($this->from))
|
|
->to(...(is_array($target) ? $target : [$target]))
|
|
->cc(...$cc)
|
|
->bcc(...$bcc)
|
|
->priority($priority)
|
|
->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;
|
|
}
|
|
|
|
/**
|
|
* 以 HTML 格式发送邮件
|
|
*
|
|
* @param Address|string|array<string|Address> $target
|
|
* @param string $subject
|
|
* @param string $html
|
|
* @param array<string> $cc
|
|
* @param array $bcc
|
|
* @param int $priority
|
|
*
|
|
* @return bool
|
|
* @throws TransportExceptionInterface
|
|
*/
|
|
public function sendHtml(
|
|
Address|string|array $target,
|
|
string $subject,
|
|
string $html,
|
|
array $cc = [],
|
|
array $bcc = [],
|
|
array $attachmentPaths = [],
|
|
array $attachments = [],
|
|
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);
|
|
|
|
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;
|
|
}
|
|
}
|