Search code examples
phpemailgmail

PHPMailer using gmail


I'm trying to send emails using PHPMailer and Gmail. It worked fine on PHP 5.5 but when I updated to php 5.6 I get this error (tls on port 587): stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

If I try using ssl on port 465 I get this instead: 2015-05-29 15:11:58 SMTP ERROR: Failed to connect to server: (0) 2015-05-29 15:11:58 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I googled around and the only thing I found out is that I should set up OAUTH2 but I don't know how. I googled around for that as well and found this: https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

I followed that and got stuck when they said that I should update get_auth_token.php. I can't find this file anywhere. This is how I installed phpmailer using composer: "phpmailer/phpmailer": "~5.2"

Do anyone know how to get it working with or without OAUTH2? I'm using php 5.6.8

Thanks in advance, Busarna4


Solution

  • PHP 5.6 introduces SSL certificate verification, So if your config is broken, it will fail with this SSL3_GET_SERVER_CERTIFICATE:certificate verify failed error. You should fix your SSL, but you can revert to the old behavior by overriding PHP MAILER and passing these options to the smtpConnect function like this way.

    $options = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    

    @Courtesy Synchro