Files
hdk/src/Utils/Service/EmailService.php
李东云 478e6066f2 feat(utils): 增加了邮件服务
Signed-off-by: 李东云 <dongyun.li@luxcreo.ai>
2022-07-26 18:21:57 +08:00

63 lines
1.4 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\Utils\Service;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
/**
* 邮箱验证码
*/
class EmailService
{
/**
* @var \Symfony\Component\Mailer\Mailer
*/
private Mailer $mailer;
public function __construct()
{
$dsn = config('common.third_party.email.dsn');
$transport = Transport::fromDsn($dsn);
$this->mailer = new Mailer($transport);
}
/**
* 发送邮件
*
* @param string $target
* @param string $subject
* @param string $text
*
* @return bool
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function sendCode(
string $target,
string $subject = '',
string $text = ''
): bool {
$from = config('mailer_sender_name') . ' <' . config('mailer_sender') . '>';
$email = (new Email())
->from(Address::create($from))
->to($target)
->subject($subject)
->text($text);
$this->mailer->send($email);
return true;
}
}