1

I'm running Windows 10 Pro with IIS hosting PHP 5.6.11. In the past, an older version of PHPMail worked perfectly for sending emails via gmail in this type of windows/IIS/PHP setup. At some point in the past it quit working. So now I'm using the very latest PHPMail library as of January 6, 2018. Here is my simple example taken mostly from this gmail example.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // TLS only
$mail->SMTPSecure = "tls"; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); 
$mail->AltBody = 'HTML messaging not supported';

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

Here is the culprit:

Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [.....\PHPMailer\src\SMTP.php line 405]

How can I solve this?

UPDATE: I found the true source of the problem and updated the error message in this post, but I don't know how to solve it.

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • Error code 0 means it cannot connect to host. Check internet connection and check smpt request module like Curl enabled? – Thamaraiselvam Jan 05 '18 at 05:29
  • Have you configured SMTP server on windows 10? If not please follow this article: https://support.office.com/en-us/article/How-to-configure-IIS-for-relay-with-Office-365-eb57abd2-3859-4e79-b721-2ed1f0f579c9 – maddy23285 Jan 05 '18 at 05:38
  • You've based your code on a very old example, so make sure you're using [the latest PHPMailer](https://github.com/PHPMailer/PHPMailer) and base your code on the gmail example provided with it. Then [read the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting). – Synchro Jan 05 '18 at 09:43
  • maddy23285 - I shouldn't have to configure smtp locally because I don't want to use my local smtp - I want to use gmail's smtp server. – HerrimanCoder Jan 05 '18 at 13:26
  • Synchro, I downloaded the latest PHPMailer, but I see no gmail example in there. Where is it? – HerrimanCoder Jan 05 '18 at 13:28
  • Two thoughts: From the comments in the ref'd gmail example code: *use $mail->Host = gethostbyname('smtp.gmail.com'); if your network does not support SMTP over IPv6* (i.e., do you support IPv6?) -- and: Do you trust the certificate? The chain is OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign -> C = US, O = Google Trust Services, CN = Google Internet Authority G3 -> C = US, ST = California, L = Mountain View, O = Google Inc, CN = smtp.gmail.com – Hagen von Eitzen Jan 06 '18 at 16:12

4 Answers4

1

There are many case you have to check like

  • open SSL in enable using you php ini
  • you have to check your firewall that you are successfully able to access gmail server.
  • etc you can find more solution and cases here
Chandraveer
  • 161
  • 1
  • 9
  • openssl doesn't exist as a setting in my IIS PHP. Any ideas? I'll google on this though. I had already been to the post you provided and I had already tried most of those. Curious about openssl though. – HerrimanCoder Jan 05 '18 at 13:12
  • look into the following link, hope it will help you out [http://www.herongyang.com/PKI/HTTPS-PHP-Configure-PHP-OpenSSL-on-Windows.html](http://www.herongyang.com/PKI/HTTPS-PHP-Configure-PHP-OpenSSL-on-Windows.html) – Chandraveer Jan 05 '18 at 13:16
  • I enabled openssl in my php.ini and restarted IIS. No joy. My firewall is turned off. I visited that page you posted, none of those suggestions worked, I believe I tried all of them. I tried TLS and port 587, same error + a bunch of unprintable chars are emitted. – HerrimanCoder Jan 05 '18 at 13:24
  • can you look into it as well for your gmail account [settings](https://stackoverflow.com/questions/21937586/phpmailer-smtp-error-password-command-failed-when-send-mail-from-my-server/25175234#25175234). – Chandraveer Jan 05 '18 at 13:29
0

Change below value in your code.

$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 2; //Get Error Details

Hope that's work.

Ashish Kadam
  • 1,439
  • 2
  • 13
  • 18
0

I have tried everything that have been written on Stackoverflow but no luck. At last I omitted the line:

$mail->isSMTP();

Of course it is not a solution but in my case I had to solve the problem and it just did it. Now I'm using php's original mail() function. By the way I tried to use Gmail and Hotmail SMTP services. I don't want anyone to spend so much time on this like me.

Mycodingproject
  • 1,031
  • 1
  • 9
  • 13
0

Everything was solved when I installed a correct certificate in IIS.

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158