邮件symfony 4不工作

问题描述 投票:0回答:2

我使用symfony 4启动了一个项目,邮件程序不起作用,但它应该很容易。在你问之前,如果我从我的代码中复制过来的登录名和密码我能够登录我的邮件帐户,我也尝试使用netcourrier邮件帐户,同时双向身份验证不活跃我允许不太安全的应用程序访问邮件帐户。这是我的conf:在我的.env中:

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

在我的控制器中:

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');}

我得到的错误是:

Connection could not be established with host smtp.gmail.com [ #0]
php swiftmailer symfony4
2个回答
0
投票

我认为这不是邮件的问题,而是gmail。我复制你的步骤尝试通过gmail的smtp服务器连接,但得到了同样的错误。在.env文件中使用不同的MAILER_URL(不同的smtp服务器)时,一切都会像它应该的那样工作。


0
投票

问题是你用谷歌连接SMTP,这是正确的:

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

我把它定义为App/Services中的服务,这是代码

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

使用它我从控制器调用它

    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="[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');
}

在Symfony 4.x上完美运行

© www.soinside.com 2019 - 2024. All rights reserved.