SMTP错误:无法连接到服务器:(0)和邮件发送成功但没有收到电子邮件

时间:2018-05-08 00:28:24

标签: php smtp phpmailer

我尝试使用PHPMailer发送电子邮件,这是我的PHP代码

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


require 'vendor/autoload.php';

$mail = new PHPMailer(true);                            
try {

    $mail->SMTPDebug = 2;                                 
    $mail->isSMTP();                                      
use SMTP
    $mail->Host = 'smtp.gmail.com';  
    $mail->SMTPAuth = true;                               
    $mail->Username = 'abc@gmail.com';                 
    $mail->Password = 'password';                           
    $mail->SMTPSecure = 'ssl';                            
    $mail->Port = 465;                                    

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('efg@gmail.com', 'Joe User');     
$mail->addAddress('hij@gmail.com');               
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');


$mail->isHTML(true);                               
$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;
}
?> 

这是我在本地运行我的PHP代码时终端的结果

 SMTP ERROR: Failed to connect to server:  (0)
 2018-05-08 00:13:48    SMTP connect() failed. 
 Message could not be sent. Mailer Error: SMTP connect() failed. 

当我注释掉$ mail-&gt; isSMTP();它说消息已成功发送,但我没有收到电子邮件 而且我不知道问题是我是否在本地运行我的代码,我需要将其放入XAMPP进行测试,或者我的SMTP配置有问题,谢谢

0 个答案:

没有答案
相关问题