dtjbcda841554 2017-04-03 10:36 采纳率: 0%
浏览 103
已采纳

PHPMailer错误使用GMAIL:邮件程序错误:SMTP连接()

I tried to send email from my localhost using my gmail account. I also tried to find answers in the web and even in this website but nothing helped me. I am using XAMPP.

This is the code:

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer();         // create a new object
$mail->IsSMTP();                 // enable SMTP
$mail->SMTPDebug = 2;            // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;          // authentication enabled
$mail->SMTPSecure = 'ssl';       // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;               // or 587
$mail->IsHTML(true);
$mail->Username = "myUser@gmail.com";
$mail->Password = "myUserPass";
$mail->SetFrom("myUser@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("OtherUser@gmail.com");

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

The full error which I got:

2017-04-03 10:33:15 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 CLIENT -> SERVER: EHLO localhost 2017-04-03 10:33:15 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [46.117.89.127] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 2017-04-03 10:33:15 CLIENT -> SERVER: AUTH LOGIN 2017-04-03 10:33:15 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2017-04-03 10:33:15 CLIENT -> SERVER: bXlVc2VyQGdtYWlsLmNvbQ== 2017-04-03 10:33:15 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2017-04-03 10:33:15 CLIENT -> SERVER: bXlVc2VyUGFzcw== 2017-04-03 10:33:15 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 SMTP Error: Could not authenticate. 2017-04-03 10:33:15 CLIENT -> SERVER: QUIT 2017-04-03 10:33:16 SERVER -> CLIENT: 221 2.0.0 closing connection p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:16 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I tried to delete that:

$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail

Or changing the port to 25/587 but it did not help. Does someone know how to fix that?

  • 写回答

2条回答 默认 最新

  • dongzan9069 2017-04-03 10:47
    关注

    Assuming that your username and password entered is correct, you need to authorize the external application use of gmail.

    Follow the next steps and authorize to make it:

    1. First, go to your Google Account Management page
    2. Under the Connected apps & sites, click Allow less secure apps: ON

    In case it does not work, if your gmail account has 2-step verification enabled, use an auto-generated password.

    Update:

    Although turning Allow less secure apps: ON might solve your problem, it should be taken as a troubleshooting tip and not as the final solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败