我正在寻找使用 Symfony Mailer在个人项目中。这个想法是用户订阅时事通讯,然后他收到一封确认他订阅的邮件。 我在我的 Controller 中创建了一个函数发送邮件,以便在提交表单时调用此函数。
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".
你知道为什么会出现这个错误吗?我不明白发生了什么。
谢谢。
最佳答案
要使用模板,您需要使用 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;
}
关于php - Symfony 5 Mailer 未定义方法名为 "htmlTemplate",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61556666/