【问题标题】:PHPMailer end of script before header标头前的 PHPMailer 脚本结束
【发布时间】:2019-03-20 22:21:32
【问题描述】:

我用一个表单编写了一个非常简单的代码,然后使用 PHPMailer 和 G-suite 获取这些信息并发送和发送电子邮件。 我已经在 xampp 上运行了这段代码,一切正常。它正在运行。但是当我把它移到我的主机上时,它并没有运行......

PD:我已经阅读了有关此错误的信息,可能与文件权限有关,但我已经尝试了所有...将所有文件设置为 755,最后我尝试将其设置为 777,但仍然无法正常工作。 .

我已与托管服务提供商联系,并表示他们可以帮助我解决这个个人问题:/

这是我的 php 代码:

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

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$name="test";
$phone="test";
$email="test";

$mail = new PHPMailer(true);  
try {

$password = 'PASSWD';

$mail->SMTPDebug = 2; 
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587;
$mail->SMTPAuth = true; 
$mail->Username = 'email@email.com'; 
$mail->Password = $password;
$mail->SMTPSecure = 'tsl'; 


$mail->setFrom('email@email.com', 'Something');
$mail->addAddress('email@email.com');
$mail->addAddress('email@email.com'); 
$mail->isHTML(true);
$mail->Subject = 'New lead';
$mail->Body = 'Name: '.$name.'<br>Email: '.$email.'<br>Phone: '.$phone;


$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

这是我得到的唯一错误:

[client IP] headers之前的脚本输出结束:send.php

警察局:

SMTPSecure 改为 tls

还是不行:(

编辑
这是我遇到的错误

117.03.2019 22:04:22 [client] AH01215:PHP 解析错误:语法错误,第 3 行出现意外的“使用”(T_USE):

17.03.2019 22:04:34 [client ] AH01215:PHP 警告:使用未定义的常量 \xe2\x80\x98display_errors\xe2\x80\x99 - 假定为 '\xe2\x80\x98display_errors\xe2\x80\x99 '(这将在 PHP 的未来版本中引发错误)在第 2 行:

17.03.2019 22:05:14 [client] AH01220: 等待 CGI 脚本输出超时

17.03.2019 22:05:14 [客户端] AH00574: ap_content_length_filter: apr_bucket_read() 失败

更新

我将 php 更新到 7.3.2。 PHPMailer 在 6.0.7 版本上。我得到的唯一不同的错误是这个......

PHP 警告:使用未定义的常量 \xe2\x80\x98display_errors\xe2\x80\x99 - 假定为 '\xe2\x80\x98display_errors\xe2\x80\x99'(这将在 PHP 的未来版本中引发错误) 在第 2 行的 /mnt/web401/c2/79/59742679/url 中:/home/strato/http/power/rid/26/79/59742679/url

更新 2

  • PHP 7.3.2
  • PHPMailer 6.0.7
  • Gsuite
  • 在 XAMPP 上测试并实际运行
  • 在 Strato 上托管

实际代码:

ini_set('display_errors', true);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer2/src/Exception.php';
require 'phpmailer2/src/PHPMailer.php';
require 'phpmailer2/src/SMTP.php';

$name="test";
$phone="test";
$email="test";

$mail = new PHPMailer(true);  
try {

$password = 'mypassword';

    $mail->SMTPDebug = 3;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'myemail';
    $mail->Password = $password;
    $mail->SMTPSecure = 'tls';
    $mail->setFrom('email', 'fromemail');
    $mail->addAddress('email');
    $mail->addAddress('email');

    $mail->isHTML(true);
    $mail->Subject = 'New lead';
    $mail->Body = 'some text';


    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我的文件:

(我正在启动 send2.php 因为它使用的是最新版本的 phpmailer[这个位于 phpmailer2])

当我执行代码时,我会在屏幕上看到这个:

2019-03-20 13:57:54 连接:打开 smtp.gmail.com:587, timeout=300, options=array()

我去我的主机面板,我只是看到这个

20.03.2019 14:55:04 my.website [client 80.38.90.0] AH01215:PHP 警告:使用未定义的常量 \xe2\x80\x98display_errors\xe2\x80\x99 - 假定为 '\xe2\x80\x98display_errors \xe2\x80\x99'(这将在 PHP 的未来版本中引发错误)在第 2 行的 my.route/send2.php 中:my.route/send2.php

20.03.2019 14:55:44 my.website [client 80.38.90.0] AH01220: 等待 CGI 脚本 my.route/send2.php 输出超时

20.03.2019 14:55:44 my.website [client 80.38.90.0] AH00574: ap_content_length_filter: apr_bucket_read() 失败

【问题讨论】:

  • 我不知道是不是这个原因,但是SMTPSecure应该是tls,而不是tsl。我建议检查错误日志。
  • @Synchro 我刚刚将 tsl 更新为 tls 但仍然收到此错误...
  • @Synchro 正如我在帖子中所说,此代码在本地主机上运行。但它不在托管上。企业没有给这种支持来帮助我。我认为这是他们的错。我在日志上遇到的唯一错误是我发布的那个....还有其他方法可以解决这个问题吗?
  • 您可以尝试SMTPDebug = 3,但我认为您在查看错误输出时遇到问题。也许尝试使用ini_set(‘display_errors’, true) 启用 php 错误显示
  • @Synchro 错误已在帖子中更新

标签: php hosting phpmailer


【解决方案1】:

ap_content_length_filter: apr_bucket_read() failed 似乎是这里的相关错误。

似乎最有可能的罪魁祸首是达到了 Apache 超时(来源:https://stackoverflow.com/a/38711063/5505001)您应该询问您的网络主机他们是否允许您临时更改 Apache 超时,看看这是否真的是问题所在。

【讨论】:

    【解决方案2】:
    PHP Parse error: syntax error, unexpected 'use'
    

    这通常意味着您在非常旧版本的 PHP 上运行新版本的 PHPMailer。确保您至少使用 PHP 5.5,但现在您应该只在 7.3 上进行新开发。

    PHP Warning: Use of undefined constant \xe2\x80\x98display_errors\xe2\x80\x99
    

    这是一个复制和粘贴错误 - 看起来 SO 将我使用的单引号更改为弯引号;把它们改回来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 2016-03-24
      • 1970-01-01
      • 2013-12-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多