如何在symfony4中发送令牌



嘿,我需要使用swift mailer发送一个令牌,消息中包含一个链接将用户重定向到可以设置新密码的页面。我的问题是,我收到的消息是页面html脚本,而不是重定向到它的链接,希望你能帮助我。

/**
* @Route("/checking_email", name="checking_email")
*/
public function CheckingEmail(Request $request,Swift_Mailer $mailer, TokenGeneratorInterface $tokenGenerator)
{
$user = new User();
$email = $request->request->get("inputEmail", "valeur par défaut si le champ n'existe pas");
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
////////////////////////////////////
if(!$user){
$var=true;
$this->addFlash("notice", "This is an error message");  
}else{
$var=false;
$user->setToken($tokenGenerator->generateToken());
//enregistrement de la date de création du token
$user->setPasswordRequestedAt(new Datetime());
$em = $this->getDoctrine()->getManager();
$em->flush();                 
$message = (new Swift_Message('try to check this link to renew ur password '))
->setFrom($user->getEmail())
->setTo('my_email@gmail.com')
->setBody($this->render('security/reset_pass.html.twig',array('user' => $user->getToken())),'text/plain');
$mailer->send($message);
//return $this->redirectToRoute("security/redifine.html.twig");
}        
return $this->render('security/reset_pass.html.twig',[
'form'=> $email,
'var'=>$var,
]);
}

我在gmail 中收到的消息

您应该指定您的邮件是HTML:

https://symfony.com/doc/current/email.html#sending-电子邮件

$message = new Swift_Message('try to check this link to renew ur password ')
->setFrom($user->getEmail())
->setTo('my_email@gmail.com')
->setBody(
$this->render('security/reset_pass.html.twig', [
'user' => $user->getToken()
]), 
'text/html'
);

相关内容

  • 没有找到相关文章

最新更新