PHPMailer发送电子邮件然后挂起(Windows, XAMPP)


PHPMailer Sends Email then Hangs (Windows, XAMPP)

当在本地主机(Windows, XAMPP)上使用PHPMailer时,电子邮件发送正常,但脚本永远挂起-不刷新

php自己的mail()函数可以正常工作,PHPMailer使用sendmail也可以正常工作,所以这只是SMTP模式下的问题。

奇怪的是,当逐步使用Xdebug时,

当我到达__destruct()

时,我在控制台中得到"致命错误:最大执行时间超过0秒"

,但是我可以通过这个步骤,这给了我刷新和错误反映在浏览器中。此外,一旦我这样做了,我可以刷新浏览器,新的电子邮件将正常发送,没有错误,没有挂起。退出调试模式,我返回到挂起行为。

注意:在php.ini:max_execution_time=60max_input_time=60

require_once "PHPMailerAutoload.php";
$to = "myemail@gmail.com";
$to_name = "Me";
$from_name = "fromName";
$from = "from@name.com";
$subject = "This is a test email from php " . strftime("%T", time());
$message = "phpmailer using smtp";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host     = "# censored #";
$mail->Port     = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Hostname = "myhost";
$mail->Username = '# censored #';
$mail->Password = '# censored #!';
$mail->FromName = $from_name;
$mail->From     = $from;
$mail->addAddress($to, $to_name);
$mail->Subject  = $subject;
$mail->Body     = $message;
$result = $mail->send();
echo $result ? 'Sent' : 'Error: ' . $mail->ErrorInfo;

我在使用PHPMailer开发本地主机服务器时遇到过类似的问题。经过大量研究,我克服这个问题的方法是手动要求PHPMailer类和SMTP类,如:

要求(". ./供应商/phpmailer phpmailer/class.phpmailer.php ');要求(". ./供应商/phpmailer/phpmailer/class.smtp.php");

使用phpmailerautolload .php不完全需要所有需要的类,并抛出异常。你是否有一个实时服务器来测试,因为我发现了很多答案,比如"在实时和完全部署时工作良好",这是一个冒险的选择。

通过手动要求文件,我设法克服了"最大执行时间"。但是,在使用窗口和XAMPP时,挂起是间歇性的,有时它会直接发送,有时脚本可能需要一段时间来处理。不知道这是否会起作用,但值得一试。

进一步,我调查了PHPMailer中的"class.smtp.php"文件,发现如下:

    $this->edebug('Connection: opened', self::DEBUG_CONNECTION);
    // SMTP server can take longer to respond, give longer timeout for first read
    // Windows does not have support for this timeout function
    if (substr(PHP_OS, 0, 3) != 'WIN') {
        $max = ini_get('max_execution_time');
        // Don't bother if unlimited
        if ($max != 0 && $timeout > $max) {
            @set_time_limit($timeout);
        }
        stream_set_timeout($this->smtp_conn, $timeout, 0);
    }

在上面的注释中定义了"Windows不支持超时函数"。也许从这种理解中,它可以帮助你排除可能性?