Search code examples
phpgmailphpmailer

PHPMailer Gmail Sign In Fails, despite setting Port to 465 tuning on "access to less secure apps" and using "DisplayUnlockCaptcha"


I'm trying to get my mailing system running. It worked perfectly fine on my local machine but now on the server the mailing seems to fail.

I've managed to get one message from gmail. It said that they have blocked the Sign In, I've said, ok that's me and tried again but this time I didn't even get an error message anymore.

UPDATE: I've done some more research and found out, that SMTP connect() failed, here is the code

$mail = new PHPMailer();

$mail->isSMTP();                                            
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPAuth   = true;
$mail->Username   = '[email protected]';
$mail->Password   = 'mypassword';

// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
// $mail->Port       = 465;

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port       = 587;

$mail->setFrom('[email protected]', 'MyWebsite Team');
$mail->AddReplyTo( '[email protected]', 'Contact MyWebsite Service' );
$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = "Membership, Please Verify Your Account";
$mail->Body = "Content";

if(!$mail->Send()) {
    echo "Error sending: " . $mail->ErrorInfo;
}

As you can see I've set the port to 456. And I've also turned on "access to less secure apps" in my account and used "DisplayUnlockCaptcha" as described here: gmail-keeps-blocking-phpmailer-sign-in

Despite all that it still doesn't work..

UPDATE: I've tried both port 456 and port 587 both just give me the SMTP connect() failed error..

There must be some way to find out why this did not work?


Solution

  • This combination will not work because it's asking for explicit TLS on a port that usually expects implicit TLS:

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 465;
    

    As the docs on this say, STARTTLS mode will not work on port 465. Either change to SMTPS mode, or change the port, that is, do either of these:

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465;
    

    or

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;