swiftmailer为什么不采用env var?

时间:2019-01-25 14:26:22

标签: symfony symfony4 swiftmailer dotenv

我正在尝试将swiftmailer配置为从我的symfony4应用,本地环境(Wamp,win10)中使用gmail发送电子邮件。

当我在config / packages / swiftmailer.yaml文件中添加所有参数时,该配置运行良好,但是当我尝试使用.env文件变量时,该配置不起作用,并且出现此错误:

14:01:11 DEBUG     [php] Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
[
  "exception" => Symfony\Component\Debug\Exception\SilencedErrorContext {
    +count: 1
    -severity: E_WARNING
    trace: {
      C:\wamp\www\crm-granger\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:267 { …}
      C:\wamp\www\crm-granger\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:58 { …}
    }
  }
]
14:01:11 DEBUG     [php] Warning: stream_socket_client(): Failed to enable crypto
14:01:11 DEBUG     [php] Warning: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error)
14:01:11 ERROR     [app] Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com [ #0]

Works:

config / packages / swiftmailer.yaml

swiftmailer:
    transport:        smtp
    username:         myemail@gmail.com
    password:         mypassword
    host:             smtp.gmail.com
    port:             465
    encryption:       ssl
    auth-mode:        login
    spool: { type: 'memory' }
    stream_options:
        ssl:
            allow_self_signed: true
            verify_peer: false
            verify_peer_name: false 

不起作用:

.env

MAILER_TRANSPORT=smtp
MAILER_HOST=smtp.gmail.com
MAILER_USERNAME=myemail@gmail.com
MAILER_PASSWORD=mypassword
MAILER_PORT=465
MAILER_ENCRYPTION=ssl

config / packages / swiftmailer.yaml

swiftmailer:
    transport:        '%env(MAILER_TRANSPORT)%'
    username:         '%env(MAILER_USERNAME)%'
    password:         '%env(MAILER_PASSWORD)%'
    host:             '%env(MAILER_HOST)%'
    port:             '%env(MAILER_PORT)%'
    encryption:       '%env(MAILER_ENCRYPTION)%'
    auth-mode:        login
    spool: { type: 'memory' }
    stream_options:
        ssl:
            allow_self_signed: true
            verify_peer: false
            verify_peer_name: false 

当我这样做时,vars出现并设置好:

php bin/console about
bin/console debug:config swiftmailer

给我这个:

Current configuration for extension with alias "swiftmailer"
============================================================

swiftmailer:
    default_mailer: default
    mailers:
        default:
            transport: '%env(MAILER_TRANSPORT)%'
            username: '%env(MAILER_USERNAME)%'
            password: '%env(MAILER_PASSWORD)%'
            host: '%env(MAILER_HOST)%'
            port: '%env(MAILER_PORT)%'
            encryption: '%env(MAILER_ENCRYPTION)%'
            auth_mode: login
            spool:
                type: memory
                path: 'C:\wamp\www\my-project\var\cache\dev/swiftmailer/spool'
                id: null
            stream_options:
                ssl:
                    allow_self_signed: true
                    verify_peer: false
                    verify_peer_name: false
            delivery_addresses:
                - myemail@gmail.com
            url: null
            command: '/usr/sbin/sendmail -bs'
            timeout: 30
            source_ip: null
            local_domain: null
            logging: true
            delivery_whitelist: {  }

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您的Yaml文件将覆盖.env文件。 因此,请保持Yaml配置。更详细,更合逻辑。

答案 1 :(得分:0)

没有愚蠢的配置解析器,并且在使用env时

        stream_options:
            ssl:
                allow_self_signed: true
                verify_peer: false
                verify_peer_name: false

还有更多被忽略...