如何配置gmail smtp和phpmailer?

How to configure gmail smtp and phpmailer?

我想知道问题出在哪里,因为我收到身份验证错误。

  1. 我已经有几个 Gmail 帐户,
  2. 我为我的网站创建了一个新网站,
  3. 我在 "Account and import" 中向我的旧 Gmail 帐户添加了一封新电子邮件。
  4. 我将新的电子邮件用户名和密码复制到 phpmailer 代码
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
//6nb5Drv;
function sendmail(){
$mail = new PHPMailer();  

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->Port = 587; // Set the SMTP port i tried and 457
$mail->Username = 'newmail@gmail.com';                // SMTP username
$mail->Password = 'newmailpass';                  // SMTP password

$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only

$mail->From = 'from@yahoo.com';
$mail->FromName = 'From';
$mail->AddAddress('to@gmail.com', 'To');  // Add a recipient

$mail->IsHTML(true);                                  // Set email format to HTML

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

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

echo 'Message has been sent';
}
sendmail();

但是我收到身份验证错误。 怎么了? 可能与凭据有关,我该如何配置 gmail smtp?

调试报告:

2015-12-04 17:56:15 CLIENT -> SERVER: EHLO www.site.co 2015-12-04 17:56:15 CLIENT -> SERVER: STARTTLS 2015-12-04 17:56:15 CLIENT -> SERVER: EHLO www.site.co 2015-12-04 17:56:15 CLIENT -> SERVER: AUTH LOGIN 2015-12-04 17:56:15 CLIENT -> SERVER: UHJlZGljdG9sb2d5 2015-12-04 17:56:15 CLIENT -> SERVER: U2dHZlB0VHZUbTZ1SW9ZMi1qTlNCQQ== 2015-12-04 17:56:17 SMTP ERROR: Password command failed: 435 4.7.8 Error: authentication failed: UGFzc3dvcmQ6 2015-12-04 17:56:17 SMTP Error: Could not authenticate. 2015-12-04 17:56:17 CLIENT -> SERVER: QUIT 2015-12-04 17:56:17 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

根据这个post: PHPMailer - SMTP ERROR: Password command failed when send mail from my server,在某些情况下,您必须指定 google 这不是可疑的 activity 并在您的帐户中激活一些 不太安全 的选项。
假设这是正确的密码...