我想问一下如何从Symfony框架,组件symfony mailer发送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!');
}发布于 2020-06-11 18:48:23
如果我没有理解您的问题,您希望使用新的symfony mailer组件发送电子邮件
前段时间我用邮件组件写了一个mailService,也许你能从中得到一些启发?
namespace App\Service;
use App\Utils\Utils;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class MailService
{
private $mailer;
/**
* MailService constructor.
*
* @param $mailer
*/
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
/**
* @param string $renderedView
* @param string $adresse
* @param string $subject
*
* @throws TransportExceptionInterface
* here $renderedview is a a twig template i used to generate my email html
*/
public function sendMail(string $renderedView, string $adresse, string $subject, string $env)
{
if ('dev' !== $env) {
$email = (new Email())
->from(your@email.com)
->to($adresse)
->subject($subject)
->html($renderedView);
$this->mailer->send($email);
}
}
}您必须根据您的邮件程序参数配置MAILER_DSN
( https://symfony.com/doc/current/components/mailer.html )
在文档中,您将了解如何处理一些常见的邮件程序或自己进行配置
祝你好运,并享受实验的乐趣:)
https://stackoverflow.com/questions/62301997
复制相似问题