我想问一下如何从Symfony框架(组件symfony邮件程序)发送PHP mail()。
"symfony/mailer": "5.0.*",
Dsn:
MAILER_Dsn=mail://localhost
控制器方法:
public function test(): Response
{
$transport = new EsmtpTransport('localhost');
$mailer = new Mailer($transport);
$username = $this->getUser()->getusername();
/** @var Users $user */
$user = $this->getDoctrine()->getRepository(Users::class)->findOneBy(['username' => $username]);
if (!$user)
return new Response("User $username not found! Email not tested.");
$to = $user->getEmail();
if ($to) {
$email = new Email();
$email->from('test@mydomain.com');
$email->to($to);
$email->subject('Test mail');
$email->text('This is test mail from ... for user ' . $to);
$mailer->send($email);
return new Response('Mail send!');
}
return new Response('Mail not sent - user email information missing!');
}