电子邮件尝试在gmail中通过PHP登录时失败

时间:2015-11-10 12:42:38

标签: php gmail phpmailer incoming-mail

我需要帮助用PHP发送电子邮件。我实际上是尝试使用PHP邮件发送电子邮件。不知何故,对于某些邮件ID,我能够发送电子邮件,但对于其他一些电子邮件ID,我无法发送。我收到一封gmail的电子邮件,说明“登录尝试已被阻止”。'有人可以帮我知道我需要在哪里更改我的Gmail帐户中的设置才能让我的PHP代码发送电子邮件?

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";
}

这适用于某些示例&#39; abc@gmail.com',但当我使用其他用户名,密码和电子邮件时说&x; xyz@gmail.com&#39;我在收件箱中收到一封电子邮件,说明尝试失败。&#39;帮助赞赏。

1 个答案:

答案 0 :(得分:6)

已通过在链接https://www.google.com/settings/security/lesssecureapps

中启用安全性较低的应用的权限来解决此问题