SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

时间:2018-03-05 18:59:08

标签: php localhost

  

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 at   您的服务,[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 = 'girish8134@gmail.com';                 // 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('girish8134@gmail.com', 'Giri');
    $mail->addAddress('girish3055@gmail.com', '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;
}

2 个答案:

答案 0 :(得分:1)

首先尝试这个&gt; https://support.google.com/accounts/answer/1064203?hl=en 禁用两步验证。

如果它不起作用,您可以随时尝试简化一些代码(并尝试ssl - port 467):

$mail->setFrom('girish8134@gmail.com');
$mail->addAddress('girish3055@gmail.com');

让我知道它是否有效。

答案 1 :(得分:0)

试试这个:

$mail->SMTPSecure = 'ssl';       // Use SSL encryption,
$mail->Port = 465                // change port to 465