mailer symfony 4 not working

梦想与她 提交于 2019-12-17 17:16:58

问题


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 log into my mail account, also i also tried with a netcourrier mail account, also the 2 way authentification is not active and i allowed less secure app to access the mail account. Here's my conf: in my .env:

MAILER_URL=gmail://*******@gmail.com:********@localhost

in my controller:

public function contact( \Swift_Mailer $mailer){

$message = (new \Swift_Message('Hello Email'))
        ->setFrom('*****@gmail.com')
        ->setTo('*******@gmail.com')
        ->setBody(
            $this->renderView(
                // templates/emails/registration.html.twig
                'email/registration.html.twig',
                array('url' => $url)
            ),
            'text/html'
        );
        $mailer->send($message);
return $this->render(
            'email/index.html.twig');}

and the error i get doing so is :

Connection could not be established with host smtp.gmail.com [ #0]

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/48230130/mailer-symfony-4-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!