Search code examples
phpsymfonysmtpgmailswiftmailer

Why I cant send a Gmail email with SwiftMailer?


When I try to send an email using swiftmailer in my Symfony project I get this exception:

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

Here's my config.yml:

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

and my parameters.yml:

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: pidev
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_encryption: ssl
    mailer_auth_mode: login
    mailer_port: 465
    mailer_host: smtp.gmail.com
    mailer_user: 'myGmailAddress'
    mailer_password: 'mypassword'
    secret: ThisTokenIsNotSoSecretChangeIt

I've allowed less secure apps on my Gmail account and disabled my antivirus and my firewall. Also, I've already tried to ping smtp.gmail.com, it works fine but I got the same problem.


Solution

  • Problem resolved, I had just to add the two following code lines:

    $options['ssl']['verify_peer'] = FALSE;
    $options['ssl']['verify_peer_name'] = FALSE;
    

    in

    vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php
    

    just after the line 263, so it will look like that:

    $options = array_merge($options, $this->_params['stream_context_options']);
    $options['ssl']['verify_peer'] = FALSE;
    $options['ssl']['verify_peer_name'] = FALSE;
    

    and now everything works fine!