Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

php - mailer symfony 4 not working

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 AppServices;


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('[email protected]')
            ->setTo($to)
            ->setBody(($texto),'text/html');
        return $this->mailer->send($message);
    }
}

And to use it I call it from the controller

    use AppServicesEnviomail;
    ........

    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="[email protected]";
        $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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...