Iris_geek
Iris_geek

Reputation: 393

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.

Upvotes: 3

Views: 6687

Answers (2)

Indunil Ramadasa
Indunil Ramadasa

Reputation: 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

Upvotes: -1

Iris_geek
Iris_geek

Reputation: 393

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

Upvotes: 10

Related Questions