在PHP中发送邮件:SMTP错误:无法进行身份验证

时间:2012-10-09 07:21:58

标签: php smtp phpmailer smtp-auth

我正在尝试在PHP发送邮件。我对localhost和server使用相同的代码。但是当我在服务器上使用代码时,它似乎不起作用:

SMTP Error: Could not authenticate. Message was not sent.Mailer error: SMTP Error: Could not authenticate.  

这是我的代码供您参考。

require("class.phpmailer.php"); // path to the PHPMailer class

$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myname"; // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "me@gmail.com";
$mail->AddAddress("sample@gmail.com");

$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}

我做了很多搜索,没有弹出任何东西。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:1)

尝试TLS和端口587:

$mail->SMTPSecure = "tls";
$mail->Host       = "smtp.gmail.com";
$mail->Port       = 587;

如您所见,您还可以在SMTPSecure变量中指定您想要SSL连接,并将主机用作smtp.gmail.com

同时尝试使用此$mail->IsSMTP();

替换此$mail->Mailer = "smtp";

答案 1 :(得分:0)

请检查一下它可能会有所帮助:
这是smtp连接工作的telnet吗?在Windows 7防火墙中阻止(默认)smtp连接所以, http://support.microsoft.com/kb/153119

答案 2 :(得分:0)

某些托管服务提供商,例如1和1阻止外发电子邮件端口,例如25,465和587. AFAIK唯一的解决方案是更改托管服务提供商。

尝试制作一个简单的php文件,只对您尝试使用的端口执行“fsokopen”,看看是否有效

答案 3 :(得分:0)

这是因为密码包含特殊字符。我尝试在像Password="djgh\^dfgfjk".之类的特殊字符之前使用“\”但是它对我没有帮助 我只是创建了新的id和简单的密码,没有任何特殊的字符,令人惊讶的是它的工作 使用没有特殊字符的密码。

答案 4 :(得分:0)

可能由于各种原因而发生。在我的情况下,它改变了

$phpmailer->isSMTP();

$phpmailer->Mailer     = 'smtp';

的工作。