Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Attempt Failed when signin through PHP in gmail

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 = "[email protected]";
    $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 '[email protected]', but when I use other username, password and email say '[email protected]' I receive an email in my inbox stating 'signin attempt failed.' Help appreciated.

like image 841
Iris_geek Avatar asked Dec 17 '25 20:12

Iris_geek


1 Answers

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

like image 113
Iris_geek Avatar answered Dec 20 '25 11:12

Iris_geek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!