Symfony swiftmailer via smtp gmail on localhost op

2019-07-16 11:33发布

After upgrading to php 5.6 (mac os x sierra) I'm not able to send mails on my local test environment.

But sadly mail delivery via swiftmailer in Symfony does not work.

This is the error:

[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

What I discovered until now:

Since php 5.6 openssl seems a requirement: http://php.net/manual/en/migration56.openssl.php

Because after the Update I was not able to use file_get_contents at all without this error, so what i did was to specify a openssl.cafile= in my php ini like I found here: https://andrewyager.com/2016/10/04/php-on-macos-sierra-cant-access-ssl-data/

Now file_get_contents works again, but I'm not able to send swiftmailer mails via smtp.

This is my swiftmailer config:

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 }

Do I have to provide my cafile on any other place to symfony/swiftmailer?

I already found this: PHP - Swiftmailer using STARTTLS and self signed certificates But to hardcode solutions like this is not an option, because I want to deploy the codebase without changing this every time. I prefer to solve this issue on a system level.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-16 12:04

You can create your own service that would implement this behaviour. Check this solution, also you can create own factory for Swift_SmtpTransport:

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

查看更多
叛逆
3楼-- · 2019-07-16 12:07

It seems, that the problem is the self-signed certificate as you are on your local machine.

You must add the following to your config.yml (or if you prefer to separate test/dev from prod in the subsequent config_dev.yml):

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

This way it should work and you have dev and prod env separated.

Look also here: https://github.com/symfony/swiftmailer-bundle/tree/master/Tests/DependencyInjection/Fixtures/config/yml

查看更多
登录 后发表回答