【问题标题】:Symfony2 SwiftMailer Connection could not be established with host smtp.gmail.com无法与主机 smtp.gmail.com 建立 Symfony2 SwiftMailer 连接
【发布时间】:2018-10-31 17:07:20
【问题描述】:

在阅读了很多解决方案后,我仍然收到此错误。

我的参数.yml

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: symfony
    database_user: root
    database_password: null
    secret: holamundo
    mailer_transport: smtp
    mailer_encryption: ssl
    mailer_port: 465
    mailer_auth_mode: login
    mailer_host: smtp.gmail.com
    mailer_user: justexample@gmail.com
    mailer_password: nottherealone

还有我的 config.yml

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode: %mailer_auth_mode%
    host: %mailer_host%
    port: %mailer_port%
    username: %mailer_user%
    password: %mailer_password%
    spool: { type: memory }

并通过命令行测试 swiftmailer...

php bin/console swiftmailer:email:send

我收到此错误:

[app] Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com [ #0] [] []

如果我 ping 'smtp.gmail.com' 我会收到回复。

我在 localhost:8000 上运行 symfony,很多人说要将 mailer_host 更改为...

mailer_host: 127.0.0.1

但是我在不同的端口上运行它,所以如果我把它放这个

mailer_host: 127.0.0.1:8000 

甚至这个

mailer_host: 127.0.0.1
mailer_port: 8000

我明白了:

ERROR     [app] Exception occurred while flushing email queue: Expected response code 220 but got code "", with message "" [] []

谢谢!

【问题讨论】:

  • 听起来您的 ISP 可能会阻止出站 SMTP。如果您不使用 PHPMailer,请不要使用它来标记问题。

标签: php symfony swiftmailer


【解决方案1】:

如果您想使用 gmail 进行测试,您的 parameter.yml 文件应如下所示

mailer_transport: gmail
mailer_host: null
mailer_user: email_address
mailer_password: 'password'

在控制器中使用它

    $messenger = \Swift_Message::newInstance();
    $messenger->setFrom(array("email_address" => "The name you want to be displayed "));

   $messenger->setTo(array($email1, $email2, ...));
   $messenger->setCc(array($email1, $email2, ...));      
   $messenger->setSubject($subject);
   $messenger->setBody($body, 'text/html');      
   $this->mailer->send($messenger);

有关在 symfony 中发送电子邮件的更多信息,请查看此处 https://symfony.com/doc/current/email.html

【讨论】:

    【解决方案2】:

    尝试停用您的防病毒软件。 Avast 阻止了我的发送电子邮件操作,而 Symfony 3 给了我同样的错误消息!! 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 2012-12-20
      • 2018-07-03
      • 2016-02-07
      • 2015-02-14
      • 2020-11-03
      相关资源
      最近更新 更多