3

I need a help in email sending in PHP. Am actually trying to send email using PHP mailer. Somehow for some of the mail ids am able to send the email but for some other email ids am unable to send. I receive an email in gmail stating 'Signin attempt prevented.' Can somebody help me know where do I need to change the settings in my gmail account in order to make my PHP code to send emails?

require("phpmailer/class.PHPMailer.php");
require("phpmailer/class.smtp.php");
$f_pointer=fopen("test.csv","r"); // file pointer
$emails = array();
while (($data = fgetcsv($f_pointer, 1000, ",")) !== FALSE) {
    $num = count($data);
    $emails[] = $data[1];
}
foreach($emails as $key=>$val){
    $mail = new PHPMailer();

    $mail->IsSMTP();                          // set mailer to use SMTP
    $mail->Host = "tls://smtp.gmail.com:587";  // specify main and backup server
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->Username = "abc";  // SMTP username
    $mail->Password = "some password"; // SMTP password

    $mail->From = "abc@gmail.com";
    $mail->FromName = "abc";
    $mail->AddAddress($val);

    $mail->Subject = "Here is the subject";
    $mail->Body    = "This is the HTML message body <b>in bold!</b>";
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";                     

    if(!$mail->Send())
    {
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }

    echo "Message has been sent";
}

This is working fine for some example 'abc@gmail.com', but when I use other username, password and email say 'xyz@gmail.com' I receive an email in my inbox stating 'signin attempt failed.' Help appreciated.

Iris_geek
  • 393
  • 5
  • 19

2 Answers2

10

It has been resolved by turning on the permission for less secure apps in the link https://www.google.com/settings/security/lesssecureapps

Iris_geek
  • 393
  • 5
  • 19
  • If you'd read [the PHPMailer docs](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting) the error message you saw pointed you at, you would have found this out faster. – Synchro Apr 05 '16 at 11:31
  • 5
    this was not enough for me so in addition to enabling "allow less secure apps", you also need to go https://accounts.google.com/DisplayUnlockCaptcha. – dang Oct 18 '16 at 08:17
-1

For anyone still having same issue, Google has blocked it now. Better have an alternative mail service.

Google has blocked third party less secure app connection

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • You don't need an _"alternative mail service."_, you only need [to generate a password for your app](https://stackoverflow.com/a/72553420/8838721). – Jaime Menendez Jun 09 '22 at 23:55