Swiftmailer from Advanced Yii 2.0 does not work with gmail

Betty Sanchez picture Betty Sanchez · Jun 25, 2015 · Viewed 8.2k times · Source

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?

Answer

saeedeh picture saeedeh · Jun 25, 2016

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.