2018-03-05 18:25:08服务器 - >客户端:220 smtp.gmail.com ESMTP o5sm27821483pfh.51 - gsmtp 2018-03-05 18:25:08客户端 - >服务器:EHLO localhost 2018-03-05 18 :25:08服务器 - >客户:250-smtp.gmail.com为您服务,[43.247.156.6] 250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2018-03-05 18:25:08客户端 - >服务器:STARTTLS 2018-03-05 18:25:08服务器 - >客户端:220 2.0.0准备启动TLS SMTP错误:无法连接到SMTP主机。 2018-03-05 18:25:09客户端 - >服务器:退出2018-03-05 18:25:09 2018-03-05 18:25:09 SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting消息已发送
我的代码是
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'Giri');
$mail->addAddress('[email protected]', 'giri3055'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message bod';
$mail->AltBody = 'This is the body is';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
首先尝试这个> https://support.google.com/accounts/answer/1064203?hl=en禁用两步验证。
如果它不起作用,你总是可以尝试简化一些代码(并尝试ssl - port 467
):
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
让我知道它是否有效。