电子邮件管道到脚本有效,但仍返回退回邮件


Email pipe to script works, but still returns bounceback mail

这是我用于拦截的PHP脚本:

#!/usr/local/bin/php -q
<?php
//Listen to incoming e-mails
$sock = fopen("php://stdin", 'r');
$email = '';
//Read e-mail into buffer
while (!feof($sock))
{
    $email .= fread($sock, 1024);
}
//Close socket
fclose($sock);
emailPoster('email@address.com', "message");
function emailPoster( $to, $email )
{
    $subject = "Email Intercepted";
    $body = $message;
    $headers    = "To: {$to}'r'n";
    $headers    .= "From: noreply@example.com'r'n";
    $headers    .= "Subject: {$subject}'r'n";
    $headers    .= "Reply-To: noreply@example.com'r'n";
    $headers    .= "Return-Path: noreply@example.com'r'n";
    $headers    .= "MIME-Version: 1.0'r'n";
    $headers    .= "Date: " . date("r") . "'r'n";
    $headers    .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
    $sender     = '-fnoreply@example.com';
    if (mail($to, $subject, $body, $headers, $sender) ){
        echo("<p>Message successfully sent!</p>");
    } else {
        echo("<p>Message delivery failed...</p>");
    }
}
?>

以及我在cPanel中使用的管道命令:

usr/local/bin/php -q /public_html/[mywebsite]/email/intercept.php

当我向适当的地址发送电子邮件时,它确实会处理拦截.php脚本,但它也会返回退回错误。

有什么想法吗?

如果将电子邮件通过管道传输到 PHP 脚本,则不能在脚本中使用"ECHO"或其他输出命令。 每个输出命令都会出错。同时从文件末尾删除"?>"。此标记后的每个字符都会生成输出标头并导致错误。

我不是使用 echo 语句,而是将其写入文件。确保目录中的写入权限设置正确。

您可以随时输入:

return NULL;

在 PHP 文件的末尾停止任何将阻止脚本反弹的返回消息。

如果在脚本中调用 CURL,则需要检查以下选项:

  • CURLOPT_VERBOSE :必须false,因为如果true它将生成绕过所有其他停止它的尝试的输出。
  • CURLOPT_HEADER :如果设置为 true,这可能会产生问题。 你的方案需要测试。

卷曲选项文档:https://www.php.net/manual/en/function.curl-setopt.php

用于设置 CURL 选项的代码是:

curl_setopt($c, CURLOPT_VERBOSE, false);
curl_setopt($c, CURLOPT_HEADER, 0);

这是对以下项目的补充:

  • 使用#!/usr/bin/php -q启动脚本
  • 禁用所有调试输出: error_reporting(E_ALL ^ E_DEPRECATED ^ E_WARNING ^ E_NOTICE);
  • 删除脚本底部的?>以确保没有意外输出。
  • Return NULL; 结束脚本,以确保不返回任何内容。

脚本顶部

#!/usr/bin/php -q
<?php
error_reporting(E_ALL ^ E_DEPRECATED ^ E_WARNING ^ E_NOTICE);
// The rest of your script follows...

脚本结束

return null;