Symfony 5 Mailer 未定义方法名为 "htmlTemplate"
Symfony 5 Mailer undefined method named "htmlTemplate"
我想在个人项目中使用 Symfony Mailer。这个想法是用户订阅时事通讯,然后他收到一封确认他订阅的邮件。
我在我的控制器中创建了一个发送邮件的函数,以便在提交表单时调用此函数。
function sendEmail(MailerInterface $mailer, $useremail)
{
$email = (new Email())
->from('mail@mail.exemple')
->to($useremail)
->subject('Great !')
->htmlTemplate('emails/signup.html.twig');
$mailer->send($email);
return $email;
}
/**
* @Route("/", name="homepage")
*/
public function index(Request $request, MailerInterface $mailer)
{
$mailing = new Mailing();
$form = $this->createForm(MailingType::class, $mailing);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$this->sendEmail($mailer , $request->request->get('mailing')['email']);
return $this->redirectToRoute('homepage');
}
当我提交表单时,一切正常,但是当它进入我的 sendEmail
函数时,出现以下错误:
Attempted to call an undefined method named "htmlTemplate" of class
"Symfony\Component\Mime\Email".
你知道为什么会出现这个错误吗?我不明白发生了什么。
谢谢。
您需要使用 'TemplatedEmail' 评分者而不是 'Email' class
要使用模板,您需要使用 docs
中所述的 TemplatedEmail
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
function sendEmail(MailerInterface $mailer, $useremail)
{
$email = (new TemplatedEmail())
->from('mail@mail.exemple')
->to($useremail)
->subject('Great !')
->htmlTemplate('emails/signup.html.twig');
$mailer->send($email);
return $email;
}
我想在个人项目中使用 Symfony Mailer。这个想法是用户订阅时事通讯,然后他收到一封确认他订阅的邮件。 我在我的控制器中创建了一个发送邮件的函数,以便在提交表单时调用此函数。
function sendEmail(MailerInterface $mailer, $useremail)
{
$email = (new Email())
->from('mail@mail.exemple')
->to($useremail)
->subject('Great !')
->htmlTemplate('emails/signup.html.twig');
$mailer->send($email);
return $email;
}
/**
* @Route("/", name="homepage")
*/
public function index(Request $request, MailerInterface $mailer)
{
$mailing = new Mailing();
$form = $this->createForm(MailingType::class, $mailing);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$this->sendEmail($mailer , $request->request->get('mailing')['email']);
return $this->redirectToRoute('homepage');
}
当我提交表单时,一切正常,但是当它进入我的 sendEmail
函数时,出现以下错误:
Attempted to call an undefined method named "htmlTemplate" of class "Symfony\Component\Mime\Email".
你知道为什么会出现这个错误吗?我不明白发生了什么。
谢谢。
您需要使用 'TemplatedEmail' 评分者而不是 'Email' class
要使用模板,您需要使用 docs
中所述的 TemplatedEmailuse Symfony\Bridge\Twig\Mime\TemplatedEmail;
function sendEmail(MailerInterface $mailer, $useremail)
{
$email = (new TemplatedEmail())
->from('mail@mail.exemple')
->to($useremail)
->subject('Great !')
->htmlTemplate('emails/signup.html.twig');
$mailer->send($email);
return $email;
}