PHPMailer - 身份验证问题


PHPMailer - authentication problems

我试图使用PHPMailer创建电子邮件服务,并且im接收和身份验证错误,用户名和密码是正确的。

这是我的代码:

<?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!";
}
?>

感谢您的帮助!

你的问题从这里开始:

$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');

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

  • http://php.net/manual/en/function.error-reporting.php

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

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

将错误报告添加到文件顶部,这将有助于查找错误

<?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->SMTP 更改为:

$mail->SMTPSecure = 'tls';

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

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

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

$mail->isSMTP();

我认为它来自您的gmail帐户。您的帐户不允许使用安全性较低的应用。您可以通过此链接打开它 谷歌安全刺痛

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