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

问题描述:

I readly feel confused why i test many many example and correct example to send email with php mailer with gmail account but it did not work,here is the error

> 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.

and my code

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

at end it's better to know i test tls & ssl with port 465 & 587 but....

我觉得很困惑,为什么我测试了许多例子和正确的例子来发送带有Gmail邮件的电子邮件和gmail帐户的电子邮件但是它 没有用,这是错误 p>

 >  2015-04-21 07:06:50客户 - > 服务器:EHLO www.ariarules.com 
 2015-04-21 07:06:50客户 - > 服务器:STARTTLS 
 2015-04-21 07:06:50客户 - > 服务器:EHLO www.ariarules.com 
 2015-04-21 07:06:50客户 - > 服务器:AUTH LOGIN 
 2015-04-21 07:06:50客户 - > 服务器:
 2015-04-21 07:06:52 SMTP错误:用户名命令失败:
 535验证数据不正确
 2015-04-21 07:06:52 SMTP错误:无法验证。
 2015-  04-21 07:06:52客户 - > 服务器:退出
 2015-04-21 07:06:52 SMTP连接()失败。
邮件程序错误:SMTP连接()失败。
  code>  pre> 
 
 

和 我的代码 p>

  $ mail = new PHPMailer();  //创建一个新对象
 $ mail-> IsSMTP();  //启用SMTP 
 $ mail-> SMTPDebug = 1;  
 $ mail-> SMTPAuth = true;  //启用身份验证
 $ mail-> SMTPSecure ='tls';  //启用安全传输GMail 
 $ mail-> Host =“smtp.gmail.com”; 
 $ mail-> Port = 587;  //或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“已发送消息”; 
} 
  code>  pre> 
 
 

最后知道更好 我测试tls& ssl与端口465& 587但.... p> div>

enable "Access for less secure apps" :

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

gmail does not allow to send emails from less secured applications. there is an option 'access for less secured apps' in google account settings. turn that on and you will be able to send emails.

i was facing same issue some days before . i found this solution , may be it works for you.Pass your values like this. its strange but worked for me.

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