mailer symfony 4 not working

前端 未结 2 1047
我在风中等你
我在风中等你 2020-12-07 06:02

I started a project using symfony 4 and the mailer doesn\'t work, however it should be easy. before you ask, if i copy past the login and password from my code i\'m able to

相关标签:
2条回答
  • 2020-12-07 06:41

    I think this is not an issue with the mailer, but with gmail. I copied your steps to try to connect through the smtp server of gmail, but got the same error. When using a different MAILER_URL (a different smtp-server) in the .env file, everything works like it should.

    0 讨论(0)
  • 2020-12-07 06:54

    The problem is your connection SMTP with google, This is correct:

    MAILER_URL=smtp://smtp.gmail.com:587?encryption=tls&username=userGmail&password=PassGmail
    

    I have it defined as a service in App/Services, this is the code

    <?php
    
    
    namespace App\Services;
    
    
    class Enviomail {
    
        private $mailer;
    
        public function __construct(\Swift_Mailer $mailer)
        {
            $this->mailer = $mailer;
        }
    
    
        public function sendEmail($to, $subject, $texto) {
            $message = (new \Swift_Message($subject))
                ->setFrom('juanitourquiza@gmail.com')
                ->setTo($to)
                ->setBody(($texto),'text/html');
            return $this->mailer->send($message);
        }
    }
    

    And to use it I call it from the controller

        use App\Services\Enviomail;
        ........
    
        public function mailsolucion(Request $request, Enviomail $enviomail) {
        if ($request->isMethod('POST')) {
            $nombre=$request->get("nombre");
            $email=$request->get("email");
            $numero=$request->get("numero");
            $empresa=$request->get("empresa");
            $solucion=$request->get("solucion");
    
            if (($nombre=="")||($email=="")||($numero=="")||($empresa=="")){
                $this->addFlash(
                    'alert alert-danger',
                    'Toda la información es obligatoria'
                );
                return $this->redirectToRoute('registro');
            }
            $emailreceptor="juanitourquiza@gmail.com";
            $asunto="Soluciones gruporadical.com";
            $texto=$this->renderView(
                'emails/soluciones.html.twig',
                array(
                    'nombre' => $nombre,
                    'email' => $email,
                    'numero' => $numero,
                    'empresa' => $empresa,
                    'solucion' => $solucion,
                )
            );
            $enviomail->sendEmail($emailreceptor,$asunto, $texto);
            $this->addFlash(
                'alert alert-success',
                'Pronto nos pondremos en contacto.'
            );
            return $this->redirectToRoute('registro');
        }
        return $this->render('AppBundle:App:contacto.html.twig');
    }
    

    Works perfect on Symfony 4.x

    0 讨论(0)
提交回复
热议问题