如何在 symfony4 中发送令牌?
how to send a token in symfony4?
嘿,我需要使用 swift 邮件程序发送令牌,并且消息包含 link
将用户重定向到可以设置新密码的页面。我的问题是我收到的消息是页面 html 脚本而不是重定向到它的 link 希望你能帮助。
/**
* @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,
]);
}
the message tha i recieve in gmail
您应该指定您的邮件在 HTML :
https://symfony.com/doc/current/email.html#sending-emails
$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'
);
嘿,我需要使用 swift 邮件程序发送令牌,并且消息包含 link 将用户重定向到可以设置新密码的页面。我的问题是我收到的消息是页面 html 脚本而不是重定向到它的 link 希望你能帮助。
/**
* @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,
]);
}
the message tha i recieve in gmail
您应该指定您的邮件在 HTML :
https://symfony.com/doc/current/email.html#sending-emails
$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'
);