Swiftmailer from Advanced Yii 2.0 does not work with gmail

10k Views Asked by At

I'm trying to configure swiftmailer in the advanced yii 2.0 template. I've gone through many posts and I understand that there are some issues with gmail. My configuration environment in development is the following:

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => gethostbyname('smtp.gmail.com'),
            'username' => '[email protected]',
            'password' => 'xxssxxxx',
            'port' => '465',
            'encryption' => 'ssl'
        ]

I've also set the support mail that's used in the controller to the same gmail address in the main-local config shown above. I've tried switching to a less secure configuration for apps in the gmail account and it did not work and I'm not particularly fond on changing this. I get the following error when I use ssl encryption

connection could not be established with host ... [ #0]

If I don't specify the encryption I get a time out error.

I have OpenSSL support enabled according to my phpinfo file but I cannot make it work. TLS does not work either because if I change the config (port:'587' & encryption='tls') I get the following error

stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Any ideas on how can I fix this? Is this an unfixed issue? Should I use another mailing extension?

6

There are 6 best solutions below

3
On

For gmail: encryption must be setted to tls, port to 587 and host to smtp.gmail.com (check if your gethostbyname('smtp.gmail.com'), get the correct value) see the sample below:

 'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => '[email protected]',
            'password' => 'yourPassword',
            'port' => '587',
            'encryption' => 'tls',
        ],
1
On

The options can be set like this:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'viewPath' => '@common/mail',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => gethostbyname('smtp.gmail.com'),
        'username' => '[email protected]',
        'password' => 'xxssxxxx',
        'port' => '465',
        'encryption' => 'ssl',
        'streamOptions' => [ 
            'ssl' => [ 
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ]
    ]

Editor's note: disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.

2
On

Adding streamOptions solved my problems!

'streamOptions' => [ 
        'ssl' => [ 
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]
0
On

Try using the gmail smtp IP

            'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => '64.233.171.108',
            'username' => '[email protected]',
            'password' => 'XXXXXXX',
            'port' => '587',
            'encryption' => 'tls',
        ],

Some production servers cannot solve FQDN via DNS servers

0
On

I had this issue my oversight being i was connecting with tls as my encryption , while the remote server only supported the older ssl encryption.

All i did was to change my encryption parameter value from "tls" to "ssl" and everything worked.

I hope this helps someone.

1
On

Please check :

  1. Internet connection
  2. email Configuration

This is my configuration. An Email succesfuly send.

 'components'=>[
        'mailer' => [
           'class' => 'yii\swiftmailer\Mailer',
           'viewPath' => '@app/mail',
           'useFileTransport' => false,
           'transport' => [
              'class'=>'Swift_SmtpTransport',
              'host'=>'smtp.gmail.com', //sample
              'username'=>'[email protected]', //sample email
              'password'=>'~!@#$%%^&&', // sample password
              'port'=>'465', // gmail ssl use port 465, 
              'encryption'=>'ssl',
           ],
       ],
    ],