PHPMailer - 身份验证问题

时间:2016-02-03 23:09:51

标签: php phpmailer

我试图用PHPMailer创建一个电子邮件服务,我收到并验证错误,用户名和密码都是正确的。

这是我的代码:

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = “myemail@gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example@gmail.com', 'Nicolas El Mir');
//Set who the message is to be sent to
$mail->addAddress('example@gmail.com', 'Nicolas Mir');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

你的问题从这里开始:

$mail->Username = “myemail@gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example@gmail.com', 'Nicolas El Mir');

你看到那些卷曲/聪明的报价? “ ” ‘

  • “myemail...
  • “my gmail password”
  • ‘example...

他们窒息了你的剧本。

$mail->Username = "myemail@gmail.com";
$mail->Password = "my gmail password";
//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'Nicolas El Mir');

如果您将错误报告设置为捕获/显示,它会告诉您有关它的信息。

我真的希望这也是你的实际代码,而不仅仅是一个糟糕的字处理器粘贴。

另外,请确保您安装了Web服务器/ PHP,并且已启用该邮件。

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:只应在暂存时进行显示错误,而不是生产。

另外,请从这个答案https://stackoverflow.com/a/16048485/中提取,这对您来说可能是同样的问题,端口号等。

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }

上面的代码已经过测试并为我工作。

可能需要$mail->SMTPSecure = 'ssl';

另外,请确保您没有为该帐户启用两步验证,因为这也会导致问题。

已更新

您可以尝试将$ mail-&gt; SMTP更改为:

$mail->SMTPSecure = 'tls';

值得注意的是,某些SMTP服务器会阻止连接。 某些SMTP服务器不支持SSL(或TLS)个连接。

并从该问题中的另一个答案https://stackoverflow.com/a/31194724/中拉出来:

解决方案是我必须删除这一行:

$mail->isSMTP();

答案 1 :(得分:0)

我认为它来自您的Gmail帐户。您的帐户不允许安全性较低的应用。您可以通过此链接Google security sttings

打开它

答案 2 :(得分:-1)

请尝试将此副本libeay32.dll和ssleay32.dll复制到windows / system2。您将在php文件夹(主文件夹)中找到这两个文件。有了这个,你可以启用PHP扩展。即curl等。如果这不起作用,请提及您的错误消息。或者查看一些youtube视频!那是合适的。谢谢。

相关问题