PHP邮件程序无法连接gmail主机

时间:2017-12-19 13:20:21

标签: php smtp

我收到此错误。我需要在我的项目中实现邮件通知功能。我正在使用PHP邮件程序。主持人只是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 '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';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'XXXXXX@gmail.com';                 // SMTP username
        $mail->Password = 'XXXXX';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('XXXXXXX@gmail.com', 'XXX');
        $mail->addAddress('XXXXXXX1@gmail.com');     // Add a recipient
       // $mail->addAddress('ellen@example.com');               // Name is optional
      //  $mail->addReplyTo('info@example.com', 'Information');
      //  $mail->addCC('cc@example.com');
      //  $mail->addBCC('bcc@example.com');

        //Attachments
       // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
      //  $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

        //Content
        //$mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'This is the test email';
        $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.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    ?>

错误:

2017-12-19 13:12:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s81sm32492847pfg.60 - gsmtp
2017-12-19 13:12:44 CLIENT -> SERVER: EHLO localhost
2017-12-19 13:12:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [106.201.62.204]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-19 13:12:45 CLIENT -> SERVER: STARTTLS
2017-12-19 13:12:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-12-19 13:12:45 CLIENT -> SERVER: QUIT
2017-12-19 13:12:45 
2017-12-19 13:12:45 
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.

0 个答案:

没有答案