Symfony swiftmailer 通过本地主机上的 smtp gmail openssl 错误

Symfony swiftmailer via smtp gmail on localhost openssl error

升级到 php 5.6 (mac os x sierra) 后,我无法在本地测试环境中发送邮件。

但遗憾的是,无法通过 Symfony 中的 swiftmailer 发送邮件。

这是错误:

[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

到目前为止我发现了什么:

因为 php 5.6 openssl 似乎是一个要求:http://php.net/manual/en/migration56.openssl.php

因为在更新之后我根本无法使用 file_get_contents 而没有这个错误,所以我所做的就是指定一个 openssl.cafile= 在我的 php ini 中,就像我在这里找到的:https://andrewyager.com/2016/10/04/php-on-macos-sierra-cant-access-ssl-data/

现在 file_get_contents 又可以工作了,但我无法通过 smtp 发送 swiftmailer 邮件。

这是我的 swiftmailer 配置:

swiftmailer:

transport:        "smtp"
host:             "smtp.gmail.com"
username:         "%mailer_user%"
password:         "%mailer_password%"
auth_mode:        login
port:             587
encryption:       tls
delivery_address: "%mailer_delivery_address%"
spool:            { type: memory }

我是否必须在任何其他地方提供我的 cafile 给 symfony/swiftmailer?

我已经找到了:PHP - Swiftmailer using STARTTLS and self signed certificates 但是像这样硬编码解决方案不是一种选择,因为我想部署代码库而不每次都更改它。我更愿意在系统层面解决这个问题。

您可以创建自己的服务来实现此行为。 检查此解决方案,您也可以为 Swift_SmtpTransport:

创建自己的工厂

https://github.com/swiftmailer/swiftmailer/issues/544#issuecomment-220103233

看来,问题出在您在本地计算机上的自签名证书上。

您必须将以下内容添加到您的 config.yml(或者如果您希望在随后的 config_dev.yml 中将 test/dev 与产品分开):

swiftmailer:
    # ... your other config
    stream_options:
      ssl:
        allow_self_signed: true
        verify_peer: false

这样它应该可以工作,并且您将开发环境和生产环境分开了。

也看这里:https://github.com/symfony/swiftmailer-bundle/tree/master/Tests/DependencyInjection/Fixtures/config/yml

它在 swiftmailer 配置中对我有用:

swiftmailer:
...
  encryption: null
...