发送电子邮件时发送PEAR邮件身份验证失败 [英] PEAR mail authentication failure when sending emails
问题描述
由于我发现PHP中的内建邮件功能有安全漏洞,所以我尝试使用PEAR。我已经安装&在我的本地主机(WAMP服务器2.2)上进行了必要的配置。但是,每次尝试发送电子邮件时,都会显示以下消息。
Since I found that the in-build mail function in PHP has security vulnerabilities I tried to use PEAR. I have installed & made the necessary configuration on my localhost (WAMP server 2.2). However each time I try to send an email the following message is displayed.
错误:认证失败[SMTP:收到无效的响应代码从服务器(代码:535,响应:5.7.8用户名和密码不被接受。了解更多在5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 gg10sm16772067pbc.46 - gsmtp )]
。
error: authentication failure [SMTP: Invalid response code received from server (code: 535, response: 5.7.8 Username and Password not accepted. Learn more at 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 gg10sm16772067pbc.46 - gsmtp)]
.
用户名和密码都是正确的我已经检查了&再次。我已经检查过在线文件其他成员的类似问题,但我仍然坚持这个问题。对此问题的任何见解都非常感激。而且这里的方式是我用来发送电子邮件的PHP代码。
Both the username and password is correct I have checked it over & over again. I have checked online documents & similar questions by other members but I am still stuck on this issue. Any insights to this problem is greatly appreciated. And by the way here is the PHP code that I used to send an email.
<?php
require "Mail.php";
$from = "myemail@here.com";
$to = "mysender@here.com";
$subject= "hiii";
$body = "\n\nEmail contents here";
$host = "ssl://smtp.gmail.com";//"smtp.gmail.com";
$port = "465";//"587";
$user = "my username";
$pass = "mypassword";
$headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
$smtp = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
$mail = @$smtp->send($to, $headers, $body);
if(PEAR::isError($mail)){
echo "error: {$mail->getMessage()}";
}else{
echo "Message sent";
}
?>
推荐答案
用于创建SMTP对象的用户名需要成为您完整的Gmail邮件地址,例如fred.flintstone@gmail.com和主机变量应该是smtp.gmail.com - 它不需要以ssl://开头
The username that you use for creating the SMTP object needs to be your full gmail email address, e.g. fred.flintstone@gmail.com and the host variable should just be "smtp.gmail.com" - it doesn't need to start with "ssl://"
这将导致发送电子邮件没有问题。 (我将你的代码放入一个名为20031009.php的文件中,修正了错误,做了一些其他更改,这样它可以与我的Gmail帐户一起运行,结果如下)。
This will result in an email being sent with no problems. (I put your code into a file named 20031009.php, fixed the errors, made a few other changes so it would work with my gmail account and ran it, the following is the result.)
$ php 20031009.php
Message sent
另一个注意事项,看起来你需要交换$ from和$的值。 :)
On another note, it looks like you need to swap around the values for $from and $to. :)
这是工作代码,其全部内容(帐户和电子邮件详细信息已更改)
This is the working code, in it's entirety (with account and email details changed back)
require "Mail.php";
$to = "mysender@here.com";
$from = "myemail@here.com"; // the email address
$subject = "hiii";
$body = "\n\nEmail contents here";
$host = "smtp.gmail.com";
$port = "587";
$user = "my username";
$pass = "mypassword";
$headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
$smtp = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
$mail = @$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)){
echo "error: {$mail->getMessage()}";
} else {
echo "Message sent";
}
这篇关于发送电子邮件时发送PEAR邮件身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!