PHPMailer有时在localhost上的gmail端口465或587上无法正常工作

时间:2017-12-05 13:08:09

标签: phpmailer

我在 LOCALHOST 上使用最新版本的 PHPMailer 6.0.2 与Gmail SMTP端口[ SSL on 465 TLS 587 ]。它运行良好但不稳定或工作正常,非常奇怪,而OpenSSL扩展在PHP Config /PHP.ini文件中也是活动的。

它返回错误" SMTP错误:无法连接到SMTP主机",有时在465或587端口上。

现在它就在587.

这是端口587上的确切错误;

2017-12-05 13:00:26 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2017-12-05 13:00:26 Connection: opened
2017-12-05 13:00:26 SMTP INBOUND: "220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp"
2017-12-05 13:00:26 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp
2017-12-05 13:00:26 CLIENT -> SERVER: EHLO localhost
2017-12-05 13:00:27 SMTP INBOUND: "250-smtp.gmail.com at your service, [110.36.136.72]"
2017-12-05 13:00:27 SMTP INBOUND: "250-SIZE 35882577"
2017-12-05 13:00:27 SMTP INBOUND: "250-8BITMIME"
2017-12-05 13:00:27 SMTP INBOUND: "250-STARTTLS"
2017-12-05 13:00:27 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2017-12-05 13:00:27 SMTP INBOUND: "250-PIPELINING"
2017-12-05 13:00:27 SMTP INBOUND: "250-CHUNKING"
2017-12-05 13:00:27 SMTP INBOUND: "250 SMTPUTF8"
2017-12-05 13:00:27 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [110.36.136.72]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-05 13:00:27 CLIENT -> SERVER: STARTTLS
2017-12-05 13:00:27 SMTP INBOUND: ""
2017-12-05 13:00:27 SERVER -> CLIENT: 
2017-12-05 13:00:27 SMTP ERROR: STARTTLS command failed: 
SMTP Error: Could not connect to SMTP host.
2017-12-05 13:00:27 SMTP NOTICE: EOF caught while checking if connected
2017-12-05 13:00:27 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting     
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

这是相关的代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;

require '../../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;

$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

$mail->Username = $from_address;
$mail->Password = $from_password;
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->SetFrom($from_address, $from_name);

foreach ($to_email_list as $to) {
    $mail->AddAddress($to);
}

$mail->AddReplyTo($from_address, $from_name);
$mail->Subject = $email_subject;

//Creating Email Body
$message = "<html>\n";
$message .= "<body>\n";
$message .= '<p>Greetings,</p>';
$message .= '<p>' . $email_message . '</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$mail->isHTML(true);
$mail->MsgHTML($message);

if(!$mail->Send()) {
    echo "On Port: " . $from_smtp_port . " </br> Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!  on Port " . $from_smtp_port . "</br>";

   foreach($to_email_list as $list){
        echo $list . "</br>";
    }

}

1 个答案:

答案 0 :(得分:1)

最后,我在troubleshooting guide.

上找到了解决方案

我向其他面临同样问题的用户推荐;请在故障排除指南中仔细查看以下要点;   - 机会TLS   - PHP 5.6证书验证失败   - cURL错误60

我的脚本在Gmail ss / 465和tls / 587上工作正常。

谢谢! @Synchro。