Search code examples
symfonysymfony1symfony-formsswiftmailersymfony4

how to send a token in symfony4?


hey i need send a token using swift mailer and the a message contain a link to redirect user to the page where he could set his new password. my problem that the message i recieve its the page html script not the link that redirect to it hope you help.

        /**
         * @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('[email protected]')
        ->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


Solution

  • You're suppose to specify that your mail is in 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('[email protected]')
            ->setBody(
              $this->render('security/reset_pass.html.twig', [
                'user' => $user->getToken()
              ]), 
              'text/html'
            );