无法理解PHPMailer调试日志

时间:2017-02-28 01:53:06

标签: php phpmailer

我正在尝试使用PHPMailer, 我已启用opensll并加载它 我使用XAMPP和PHPStorm

    <?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587; //ssl : 465 --- tls:587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "te.professionnel@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "*****";

//Set who the message is to be sent from
$mail->setFrom('te.professionnel@gmail.com', 'Abou May');

//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('abou.may@gmail.com', 'Abou May');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}


 I have read lots of documentation, bu I cannot figure out the reason of this error.

这是我的代码

Team

有人可以给我一个示例代码吗?

有什么问题?

我阅读了有关故障排除的所有部分

&#34; SMTP错误:无法连接到SMTP主机。&#34;

这可能也显示为SMTP connect()失败或Called Mail()而未在调试输出中连接。这通常被报告为PHPMailer问题,但它几乎总是由于本地DNS故障,防火墙阻塞(例如GoDaddy)或本地网络上的其他问题。这意味着PHPMailer无法联系您在Host属性中指定的SMTP服务器,但并未确切说明原因。它也可能是因为没有加载openssl扩展名(参见下面的加密说明)。

2 个答案:

答案 0 :(得分:0)

我之前也遇到过这个问题。这不是PhpMailer的问题。但事实上,这是因为Gmail smtp被拒绝连接,因此你无法连接。 为了解决这个问题,你需要

{p> Google Developer Console中配置OAuth2应用。 步骤为here

如果您仍无法解决问题,我建议您使用Postmark作为邮件服务器,设置连接要容易得多。

Here是设置Postmark的指南。对我来说要容易得多。希望它有所帮助。

答案 1 :(得分:0)

这与gmail或oauth无关。

问题在于这一行:

CLIENT -> SERVER: EHLO PhpStorm 2016.1.2

当PHPMailer的SMTP客户端向这样的服务器打招呼时,默认情况下它会传递内部方法serverHostname()返回的名称。通常这会返回类似localhost.localdomainmymac.local(即真正的主机名)的内容,但如果它不可用,它会尝试找出要使用的内容 - 以及其中的一个内容回归到$_SERVER['SERVER_NAME']超级全球,在这种情况下包含PhpStorm 2016.1.2(我猜是因为您正在使用PHPStorm的内置网络服务器进行测试?) ,这不是有效的主机名,因此错误。这不是PHPStorm的一个很好的举动,可能值得作为一个bug报告。

幸运的是,您可以使用Helo属性覆盖客户端主机名的自动确定,该属性恰好存在于此类情况下。就这样做:

$mail->Helo = 'my.host.name';

代替您的真实主机名,或者通过调用其他提供可用结果的函数。

相关问题