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

时间:2015-07-19 09:02:05

标签: php email phpmailer

在localhost(Windows,XAMPP)上使用PHPMailer时,电子邮件发送正常但脚本永远挂起 - 无需刷新。

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

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

  

当我到达__destruct()

时,控制台中出现“致命错误:超过0秒的最长执行时间”

虽然我可以单步执行此操作,但这会让我在浏览器中反映出刷新和错误。此外,一旦我完成了这一点,我可以刷新浏览器,新的电子邮件将正常发送,没有错误,也没有挂起。退出调试模式后,我将返回挂起行为。

注意:在php.ini中: max_execution_time=60 max_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;

1 个答案:

答案 0 :(得分:0)

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

需要(' ../供应商/ PHPMailer的/ PHPMailer的/ class.phpmailer.php&#39); 要求(" ../供应商/ PHPMailer的/ PHPMailer的/ class.smtp.php&#34);

由于使用PHPMailerAutoload.php并不完全需要所有需要的类并抛出异常。你有一个真实的服务器进行测试,因为我发现了许多答案,例如"在现场和完全部署时工作正常#34;这是一个冒险的选择。

我设法克服了最长的执行时间"通过手动要求文件。但悬挂是间歇性的,同时使用Windows和XAMPP,有时它会立即发送,有时可能需要一段时间才能处理脚本。不确定这是否有效,但值得一试。

除此之外,我调查了" class.smtp.php" PHPMailer中的文件,发现了这个:

    $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不支持超时功能"。也许从这种理解中它可以帮助你消除可能性?