11

Based on the example that PHPMailer provides i have the script below,

date_default_timezone_set('Etc/UTC');
require './PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "********";
$mail->setFrom('[email protected]', 'First Last');
$mail->addReplyTo('[email protected]', 'First Last');
$mail->addAddress('[email protected]', 'first last');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = "example";
$mail->AltBody = 'This is a plain-text message body';

if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Even if that is the exactly the same as the original example, i cannot get it to work.

The error that i get is

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /opt/lampp/htdocs/webmail_client_practise/class.smtp.php on line 344 SMTP Error: Could not connect to SMTP host.

Notice: The OpenSSL extension in my php.ini file is already opened.

4 Answers 4

31

This is because you're running PHP 5.6 and it's verifying your certs, but your server is presenting invalid certs so it's failing. Both PHPMailer and PHP are correct in what they are doing - the code is not at fault. You can either fix your mail server, or do what it suggests in the troubleshooting guide, which is:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

And as the guide says, you should not do this unless you have to - it's compromising your security.

3
  • Now every makes sense now! Thank you Synchro!
    – Makis
    Commented Sep 21, 2015 at 12:00
  • NO - the right way to fix certificate errors is to install the certificate authority's signing cert in openssl - gagravarr.org/writing/openssl-certs/others.shtml#ca-openssl
    – symcbean
    Commented Dec 7, 2017 at 15:13
  • Did you actually read this answer, such as the bit where it says "you should not do this"? This answer is from a couple of years ago, when the most common cause of this error was not a missing or expired CA cert, but because the ISP was transparently redirecting SMTP traffic to their own server, causing certificate name mismatches, a specific case in which it's more reasonable to suppress the error in the way suggested. Just recently (Dec 2017), an out of date CA cert has been triggering the same error for gmail, which indeed would be better fixed by updating the CA cert, not suppressing.
    – Synchro
    Commented Dec 7, 2017 at 18:10
11

I was experiencing the exact same error as in the original question (yes 4 years later), and was able to solve it by changing the following within class.smtp.php (in both /includes/classes and /admin/includes/classes if they exist -- otherwise only in the PHPMailer main directory).

Change:

STREAM_CRYPTO_METHOD_TLS_CLIENT

to:

STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT

It has to do with the updating of the TLS version in more recent PHP versions.

Source: https://forums.oscommerce.com/topic/410367-phpmailer-tls-12/

4
  • 1
    This should be the approved answer! Not compromising but improving security! +1
    – Thilo
    Commented Nov 11, 2021 at 13:50
  • This was my problem as well - sending email via smtp.office365.com sometimes worked and sometimes didn't. It was completely random. This was my fix as well. Commented Dec 19, 2021 at 0:14
  • i also had the same issue with codeigniter-3. when trying to send email for mailtrap. now this solution worked for me
    – Nasik Ahd
    Commented Feb 13, 2023 at 7:46
  • Had the same issue & this was the correct answer
    – BlissSol
    Commented Feb 5 at 13:20
1

Great!

It works fine for me too.

I updated PHP Version 5.5.9-1ubuntu4.14 (mail was working fine) to and PHP Version 5.6.40-52+ubuntu20.04.1+deb.sury.org+1 (mail getting fail)

Now I just updated :- STREAM_CRYPTO_METHOD_TLS_CLIENT

TO

STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT

Now the mail is working fine.

1
  • It looks like this is intended to be a comment on an answer to this question. If an answer fixes your problem and you have helpful details of how you used the answer that you want to share, at 50 reputation you will be able to leave a comment with these details. Until then, comments on other answers shouldn't be submitted as new answers. You can get reputation by providing useful and detailed answers, asking clear and detailed questions, and suggesting helpful edits.
    – Paul
    Commented Sep 29, 2021 at 14:37
0

I solved a similar problem by reinstalling the ca-certificates package on my ubuntu server with:

sudo apt-get install --reinstall ca-certificates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.