PHPMailer仅在SMTPDebug=true时发送电子邮件

PHPMailer仅在SMTPDebug=true时发送电子邮件,php,debugging,smtp,phpmailer,Php,Debugging,Smtp,Phpmailer,我用的是PHPmailer。当$mail->SMTPDebug=true时,它工作;但当我移除那条线时,它就悄无声息地失败了。我说,默默地失败,因为它没有给出任何错误,但电子邮件似乎没有送达 $mail = new PHPMailer; $mail->SMTPDebug = true; $mail->SMTPAuth = true; $mail->CharSet = 'utf-8'; $mail-

我用的是PHPmailer。当$mail->SMTPDebug=true时,它工作;但当我移除那条线时,它就悄无声息地失败了。我说,默默地失败,因为它没有给出任何错误,但电子邮件似乎没有送达

        $mail = new PHPMailer;

        $mail->SMTPDebug = true;
        $mail->SMTPAuth = true;
        $mail->CharSet = 'utf-8';
        $mail->SMTPSecure = 'ssl';
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = '465';
        $mail->Username = 'xxxxx@gmail.com';
        $mail->Password = 'xxxxx';
        $mail->Mailer = 'smtp';
        $mail->AddReplyTo('support@xxxxx.com', 'xxxxx Support');
        $mail->From = 'xxxxx@gmail.com';
        $mail->FromName = 'xxxxx Applications';
        $mail->Sender = 'xxxxx Applications';
        $mail->Priority = 3;

        //To us
        $mail->AddAddress('xxxxx@xxxxx.com', 'xxxxx xxxxx');
        //To Applicant
        $mail->AddAddress($email, $first_name.''.$last_name);
        $mail->IsHTML(true);

        $last_4_digits = substr($card_num, -4);
        //build email contents

        $mail->Subject = 'Application From '.$first_name.' '.$last_name;
        $mail->Body    = $body_html;
        $mail->AltBody = $body_text;

        if(!$mail->send()) {
           echo 'Message could not be sent.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
通过设置

        $mail->SMTPDebug = false;

与其完全省略该行,它每次都能工作。

我认为您使用的是过时的phpmailer版本,请使用composer安装。检查PHP邮件程序的版本是6.4+

下面是如何使用composer安装最新版本的示例

composer require phpmailer/phpmailer
当您查看官方存储库代码行402时

您可以看到$SMTPDebug被设置为0,因此它不能成为它以静默方式失败的原因

public $SMTPDebug = 0;
下面提供了使用phpmailer发送电子邮件的示例指南。 这个例子在没有使用SMTPDEBUG的情况下对我有效。此外,PHPMailer(true)使异常处于启用状态,这可能很有用,这样它就不会无声地失败。

    //Load Composer's autoloader
    require 'vendor/autoload.php';
    
    //Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);
    
    try {
       
 //Server settings


        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = 'user@example.com';                     //SMTP username
        $mail->Password   = 'secret';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    
        //Recipients
        $mail->setFrom('from@example.com', 'Mailer');
        $mail->addAddress('joe@example.net', 'Joe User');     //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 = '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}";
    }
//加载作曲家的自动加载器
需要“vendor/autoload.php”;
//实例化并传递'true'将启用异常
$mail=新的PHPMailer(true);
试一试{
//服务器设置
$mail->isSMTP();//使用SMTP发送
$mail->Host='smtp.example.com';//设置要通过的smtp服务器
$mail->SMTPAuth=true;//启用SMTP身份验证
$mail->Username=user@example.com“;//SMTP用户名
$mail->Password='secret';//SMTP密码
$mail->SMTPSecure=PHPMailer::ENCRYPTION\u STARTTLS;//启用TLS加密;`PHPMailer::ENCRYPTION\u SMTPS`
$mail->Port=587;//要连接的TCP端口,请将465用于上面的'PHPMailer::ENCRYPTION_SMTPS'
//接受者
$mail->setFrom('from@example.com","梅勒",;
$mail->addAddress('joe@example.net“,”Joe User');//添加收件人
$mail->addAddress('ellen@example.com');//名称是可选的
$mail->addReplyTo('info@example.com","信息",;
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//附件
$mail->addAttachment('/var/tmp/file.tar.gz');//添加附件
$mail->addAttachment('/tmp/image.jpg','new.jpg');//可选名称
//内容
$mail->isHTML(true);//将电子邮件格式设置为HTML
$mail->Subject='主题在这里';
$mail->Body='这是以粗体显示的HTML邮件正文!';
$mail->AltBody='这是非HTML邮件客户端的纯文本正文';
$mail->send();
回音“消息已发送”;
}捕获(例外$e){
echo“无法发送邮件。邮件程序错误:{$mail->ErrorInfo}”;
}

为问题提供更多细节:

省略SMTPDebug信息时不发送电子邮件的原因是什么

由于未指定使用的PHPMailer版本,我尝试使用2013年9月12日发布的问题时的最新版本,但我也尝试了最旧的非开发版本(可在PackageGist上获得)以及介于两者之间的版本(v5.2.4、v5.2.5和v5.2.6)

我无法复制该问题,因此很可能是由于在PHPMailer中省略了
SMTPDebug
参数而导致的,除非其他依赖项、操作系统或PHP版本在该问题中起了作用,但如果没有详细信息,则无法确定

在这些旧版本中,参数是这样使用的

/**
*SMTP类调试输出模式。
*选项:0=关闭,1=命令,2=命令和数据
*@type int
*@请参阅SMTP::$do\u debug
*/
public$SMTPDebug=0;
...
受保护函数($str)
{
如果(!$this->SMTPDebug){
返回;
}
...
其中用于简单调试:

$this->edebug($e->getMessage()。“\n”);
因此,由于省略
SMTPDebug
,很难出现导致所述效果的bug


如果bounty placer仍然有问题,请尝试或提出一个新问题,提供一个新的问题。

我也遇到过同样的问题,将该行保留在那里有什么问题?因为它会在屏幕上显示一个巨大的长调试?或者有没有办法禁用它?您是否对SMTP邮件程序类文件进行了任何更改?我没有。就像我说的那样如果启用了调试,电子邮件会立即发送。如果您使用单独的文件发送smtp,那么它是否会抛出调试就无关紧要了。试试看?不确定,谢谢您的回复,但7年前有人问过它;)@user308827这对您有用吗?