无法使用带有 gmail 帐户的 phpmailer 发送电子邮件

unable to send email with phpmailer with gmail account

我很困惑为什么我测试了很多示例和正确的示例以使用 php 邮件程序和 gmail 帐户发送电子邮件但是它不起作用,这是错误

> 2015-04-21 07:06:50   CLIENT -> SERVER: EHLO www.ariarules.com
 2015-04-21 07:06:50    CLIENT -> SERVER: STARTTLS
 2015-04-21 07:06:50    CLIENT -> SERVER: EHLO www.ariarules.com
 2015-04-21 07:06:50    CLIENT -> SERVER: AUTH LOGIN
 2015-04-21 07:06:50    CLIENT -> SERVER:
 2015-04-21 07:06:52    SMTP ERROR: Username command failed:   
 535          Incorrect authentication data
 2015-04-21 07:06:52    SMTP Error: Could not authenticate.
 2015-04-21 07:06:52    CLIENT -> SERVER: QUIT
 2015-04-21 07:06:52    SMTP connect() failed.
 Mailer Error: SMTP connect() failed.

和我的代码

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "googleaccount@gmail.com";
$mail->Password = "pass";
$mail->SetFrom("user@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("user@gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

最后最好知道我用端口 465 和 587 测试了 tls 和 ssl 但是....

gmail 不允许从安全性较低的应用程序发送电子邮件。 google 帐户设置中有一个选项 'access for less secured apps'。打开它,您将能够发送电子邮件。

几天前我遇到了同样的问题。我找到了这个解决方案,可能它适用于 you.Pass 你这样的价值观。这很奇怪,但对我有用。

$username="googleaccount@gmail.com";
$password="pass":
$mail->Username = $username;
$mail->Password = $password;

启用"Access for less secure apps":

https://www.google.com/settings/security/lesssecureapps