【问题标题】:mailer symfony 4 not working邮件 symfony 4 不工作
【发布时间】:2019-04-06 20:44:04
【问题描述】:

我使用 symfony 4 开始了一个项目,但邮件程序不起作用,但它应该很容易。 在你问之前,如果我从我的代码中复制过去的登录名和密码,我就可以登录我的邮件帐户,我也尝试使用 netcourier 邮件帐户,2 路身份验证也没有激活,我允许不太安全的应用程序访问邮件帐户。 这是我的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]

【问题讨论】:

  • 您需要出示$mailer代码
  • 显示您的邮件参数
  • 我编辑了帖子,添加了您请求的 $mailer 代码
  • 我遵循了这个教程:symfony.com/doc/current/email.html
  • 您在尝试使用symfony 命令发送电子邮件时是否遇到同样的错误?

标签: php swiftmailer symfony4


【解决方案1】:

问题是您与 google 的连接 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('juanitourquiza@gmail.com')
            ->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="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');
}

在 Symfony 4.x 上完美运行

【讨论】:

    【解决方案2】:

    我认为这不是邮件程序的问题,而是 gmail 的问题。我复制了您的步骤以尝试通过 gmail 的 smtp 服务器进行连接,但遇到了同样的错误。在 .env 文件中使用不同的 MAILER_URL(不同的 smtp 服务器)时,一切正常。

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 2018-05-20
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2019-12-23
      相关资源
      最近更新 更多