【问题标题】:PHPMailer + smtp.gmail.com : Could not connect to SMTP hostPHPMailer + smtp.gmail.com:无法连接到 SMTP 主机
【发布时间】:2018-04-07 00:56:37
【问题描述】:

我试图使用 PHPMailer 库来发送电子邮件。 我使用 composer 安装了 PHPMailer 我只是使用他们的教程示例。 在此之前,我还将 gmail 帐户设置为允许不太安全的应用程序。 这是我的代码:

<?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 'phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true);                 // 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:587';      // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                  // Enable SMTP authentication
    $mail->Username = 'hidden@gmail.com';    // SMTP username
    $mail->Password = 'hidden';              // SMTP password
    $mail->SMTPSecure = 'tls';               // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                       // TCP port to connect to

    //Recipients
    $mail->setFrom('hidden@gmail.com', 'Mr. From');
    $mail->addAddress('to.@gmail.com', 'Mr. To');   // 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 body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

?>

这是错误:

2018-03-30 05:44:49 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 5sm16638786pfh.133 - gsmtp
2018-03-30 05:44:49 CLIENT -> SERVER: EHLO localhost
2018-03-30 05:44:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [123.244.104.122]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-03-30 05:44:50 CLIENT -> SERVER: STARTTLS
2018-03-30 05:44:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-03-30 05:44:50 CLIENT -> SERVER: QUIT
2018-03-30 05:44:50
2018-03-30 05:44:50
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

请帮帮我。

【问题讨论】:

标签: phpmailer


【解决方案1】:

只需在行代码$mail-&gt;SMTPDebug 之前添加以下代码即可节省时间

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

【讨论】:

  • 这通过创建一个安全漏洞“挽救了一天”。不要这样做。正确解决问题。说明在 PHPMailer 故障排除指南中。
猜你喜欢
  • 2012-05-24
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多