-1

First, I already read this stack post, and it doesn't seem to answer my question. I'm unclear how to apply those findings to my situation.

The following code works fine on my LAMP server (Bluehost), and I receive the email:

require_once("PHPMailer/src/Exception.php");
require_once("PHPMailer/src/PHPMailer.php");
require_once("PHPMailer/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->SMTPDebug = 2; // verbose debug output
//$mail->SMTPDebug = 0; // no output
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = $mailSender;
$mail->Password = $mailSenderPassword;
$mail->SetFrom($mailSender);
$mail->addAddress($mailTo);
$mail->isHTML(true);
$mail->Subject = "email test";
$mail->Body    = "testing an email";
$mail->send();

But on my local WIMP (Windows-IIS-MySQL-PHP) PC, I always get the following error when running it:

Failed to connect to server: (0)

Note: I run php pages successfully all the time on my WIMP pc. The only thing that doesn't work locally is PHPMailer.

I tried turning my windows firewall completely off and that made no change.

How can I run this successfully on my Windows 10 IIS PC?

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • Looks like a random downvoter ran through, downvoting everyone without reasons... – HerrimanCoder Oct 03 '19 at 15:15
  • What environment on windows you are running those scripts? Is it a docker or what? Can you curl or fsockopen smtp.gmail.com from php to confirm it has a network connection there? – yergo Oct 03 '19 at 16:14

3 Answers3

1

This was the solution:

My cafile set in php.ini had the wrong path to my cert:

openssl.cafile=wrongpath\cacert.pem

I fixed the path and everything worked.

As a stop-gap, failing everything else, the following will work:

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

Taken from here:

PHPMailer: SMTP Error: Could not connect to SMTP host

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • Eep, NO! That is not a "solution". It's hiding the problem so that you don't get to find out about security issues, or someone intercepting your traffic, defeating the whole point of using TLS. The right way to fix TLS problems is to [read the guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting) which tells you how to diagnose and fix them. – Synchro Oct 04 '19 at 15:46
  • In that case disable encryption altogether: `$mail->SMTPSecure = false;`, and `$mail->SMTPAutoTLS = false;`, then you won't need to suppress errors like that. – Synchro Oct 04 '19 at 16:15
  • Actually that makes no sense - you're talking to gmail which will never respond with an invalid certificate no matter where you're connecting from, so it means your local system is broken - following the guide will help you fix it; probably something like outdated CA certs. – Synchro Oct 04 '19 at 16:18
  • Don't forget to remove the downvote from the original post. – HerrimanCoder Oct 04 '19 at 16:24
  • That wasn't me. – Synchro Oct 04 '19 at 16:25
0

It sounds like your PHP is running just fine, as is PHPMailer — but you have some network problem that means that PHP can't talk to your mail server. PHP scripts that don't talk to mail servers obviously don't have any problem with that. Have a read of the PHPMailer troubleshooting guide which has some techniques for diagnosing network problems like this, though you may need to adapt them for Windows.

Since you're sending via gmail, I recommend that you base your code on the gmail example provided too — your code lacks any debug output or error handling, so you don't have any feedback on what's going wrong.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Synchro, what do you mean the code lacks any debug output? I have `$mail->SMTPDebug = 2;` – HerrimanCoder Oct 04 '19 at 15:29
  • Ah, indeed you do, but you didn't show the actual debug output. For diagnosing connection problems, `SMTPDebug = 3` is more appropriate. – Synchro Oct 04 '19 at 15:42
-1

From first view I saw these, that you have written some props witout uppercases. That should be like:

 IsSMTP()
 AddAddress()
 IsHTML()
 Send()

Also I don't remember about this block much now, but before I've implemented something like this, and it worked. If it can help you, I'll share that code here (sending confirmation email):

use PHPMailer\PHPMailer\Exception as PhpMailerException;
use PHPMailer\PHPMailer\PHPMailer;

// ...

public function sendEmail_PhpMailer($to_email, $from_email, $name, $confirm_token): array
{
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        // $mail->SMTPDebug = 2; // Enable verbose debug output
        // $mail->isSMTP(); // Set mailer to use SMTP // https://github.com/PHPMailer/PHPMailer/issues/1209#issuecomment-338898794
        $mail->Host = config('custom.phpmailer.host'); // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;
        $mail->Username = config('custom.phpmailer.username'); // SMTP username
        $mail->Password = config('custom.phpmailer.password'); // SMTP password
        $mail->SMTPSecure = config('custom.phpmailer.secure'); // Enable TLS encryption, `ssl` also accepted
        $mail->Port = config('custom.phpmailer.port'); // TCP port to connect to

        $mail->setFrom($from_email, config('app.name'));
        $mail->addAddress($to_email, $name);
        // $mail->addReplyTo($from_email, config('app.name'));

        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Confirm Your Registration!';
        $mail->Body = "<a href=" . route('web.email_confirm', $confirm_token) . "><b>Click here to confirm your " . config('app.name') . " account!</b></a>";
        $mail->AltBody = "Confirm your account with this link: " . route('web.email_confirm', $confirm_token);

        $mail->send();

        return [
            'error' => false,
            'message' => 'Message successfully sent!',
        ];
    }
    catch (PhpMailerException $e) {
        return [
            'error' => true,
            'message' => 'Something went wrong: ', $mail->ErrorInfo,
        ];
    }
 }
boolfalse
  • 1,892
  • 2
  • 13
  • 14
  • Wasn't me, but what you said about capitalisation is simply wrong. If you comment out `isSMTP()`, it won't use SMTP, and none of the SMTP-related settings will apply. – Synchro Oct 04 '19 at 15:45