Google SMTP Error: Could not authenticate

2019-09-11 01:03发布

问题:

The following code does not work once i put it on the server, I have similar code working on my local environment and it works perfectly, any ideas?

edit: i did set my gmail setting to "less secure"

<?php
$setid = $_POST['setid'];
$promo = $_POST['promo'];


echo "Good Sir, your set ID is ".$setid.", and you are eligible for the following deal:";
echo "<br><br>";
echo $promo;

$message=  "Good Sir, your set ID is ".$setid.", and you are eligible for the following deal:"."<br><br>".$promo;

    require "phpmailer/class.phpmailer.php";

    // Instantiate Class  
    $mail = new PHPMailer();

    // Set up SMTP  
    $mail->IsSMTP();               
    $mail->SMTPAuth = true;         
    $mail->SMTPSecure = "ssl"; 
    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 465; 
    $mail->Encoding = '7bit';

    // Authentication  
    $mail->Username   = "xxx@example.com";
    $mail->Password   = "mypass";

    // Compose
    $mail->SetFrom("jghh@ghh.ca");
    $mail->AddReplyTo("ghh@ghh.ca");
    $mail->Subject = "TryIt";
    $mail->MsgHTML($message);

    // Send To  
    $mail->AddAddress("receiver@hotmail.com", "Recipient Name");
    $result = $mail->Send();
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';     

    unset($mail);



?>

This is what I get in Network -> Preview:

Good Sir, your set ID is 100065, and you are eligible for the following deal:

Current Promotion: Enjoy 15% discount on your next visit!SMTP Error: Could not authenticate.

回答1:

You probably need to enable less secure apps


Change account access for less secure apps

To help keep Google Apps users' accounts secure, we may block less secure apps from accessing Google Apps accounts. As a Google Apps user, you will see a "Password incorrect" error when trying to sign in. If this is the case, you have two options:

  1. Option 1: Upgrade to a more secure app that uses the most up to date security measures. All Google products, like Gmail, use the latest security measures.
  2. Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:

    2.1. Go to the "Less secure apps" section in My Account

    2.2. Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

If you still can't sign in to your account, the "password incorrect" error might be caused by a different reason.

SRC: https://support.google.com/accounts/answer/6010255?hl=en


UPDATE:

Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1);

and enable debug on PHPMAILER

$mail->SMTPDebug  = 1; // enables SMTP debug information (for testing)
                       // 1 = errors and messages
                       // 2 = messages only

to see if it yields anything.