PHP mail() 错误:在 additional_header 中发现多个换行符或格式错误的换行符

Error with PHP mail(): Multiple or malformed newlines found in additional_header

在没有对脚本进行任何更改的情况下突然开始收到上述错误。

主机是1and1(我知道...)

脚本在不同的服务器上仍然可以正常工作,所以我怀疑一定是服务器配置发生了一些变化导致了这种情况,尽管主机辩称不知道。

我在 Google 中找不到关于上述错误的任何信息 - 有人有任何想法吗?如果有帮助,服务器是 运行 Apache。

这很可能是有人试图利用您的代码注入电子邮件 headers。

http://resources.infosecinstitute.com/email-injection/

我建议您检查访问日志等并寻找异常 activity。您收到错误消息的事实很有希望意味着您的脚本没有受到损害,而是出错了。不过你需要确定一下。

有同样的问题: 从 header 中删除了 MIME 边界和消息,一切正常。

    $header = "From: ".$from_name." <".$from_mail.">\n";
    $header .= "Reply-To: ".$replyto."\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
    $emessage= "--".$uid."\n";
    $emessage.= "Content-type:text/plain; charset=iso-8859-1\n";
    $emessage.= "Content-Transfer-Encoding: 7bit\n\n";
    $emessage .= $message."\n\n";
    $emessage.= "--".$uid."\n";
    $emessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use different content types here
    $emessage .= "Content-Transfer-Encoding: base64\n";
    $emessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
    $emessage .= $content."\n\n";
    $emessage .= "--".$uid."--";
    mail($mailto,$subject,$emessage,$header);

遇到了类似的问题。
它突然出现了。否 PHP 代码已更改。

更改内容:PHP 已将 5.5.25-1 升级到 5.5.26。

PHP mail() 函数中的安全风险已修复,additional_headers 中不再允许额外的换行符。因为额外的换行符意味着:现在开始电子邮件消息(我们当然不希望有人通过 headers 插入一些换行符,然后是一条恶意消息)。

以前的效果很好,例如只是在 headers 之后有额外的换行符或者甚至将整个消息传递给 additional_headers,将不再起作用。

解决方案

  • 消毒您的 headers。 additional_headers 参数中没有多个换行符。这些算作 "multiple or malformed newlines":\r\r, \r[=14=], \r\n\r\n, \n\n, \n[=14=].
  • 仅对 headers 使用 additional_headers。电子邮件(多部分或多部分,有 ir 没有附件等)属于 message 参数,而不属于 headers。

PHP 安全错误报告:https://bugs.php.net/bug.php?id=68776
C 代码差异如何固定:http://git.php.net/?p=php-src.git;a=blobdiff;f=ext/standard/mail.c;h=448013a472a3466245e64b1cb37a9d1b0f7c007e;hp=1ebc8fecb7ef4c266a341cdc701f0686d6482242;hb=9d168b863e007c4e15ebe4d2eecabdf8b0582e30;hpb=eee8b6c33fc968ef8c496db8fb54e8c9d9d5a8f9

另一种带来相同新错误的情况是,如果您没有向 "mail" 命令发送任何 headers。它过去只使用默认值,现在给出误导性错误:"Multiple or malformed newlines found in additional_header".

可以通过添加以下内容来修复:

$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "Reply-To: ".$replyto."\n";
$header .= "MIME-Version: 1.0\n";

...

mail($mailto,$subject,$emessage,$header);

如果您没有做任何愚蠢的事情(即忘记清理 headers),您可能 运行 变成 Bug #69874 Can't set empty additional_headers for mail()

测试错误

$ php -d display_errors=1 -d display_startup_errors=1 -d error_reporting=30719 -r 'mail("test@email.com","Subject Here", "Message Here",NULL);'

Warning: mail(): Multiple or malformed newlines found in additional_header in Command line code on line 1

或者,如果您知道您的 PHP 版本(提示:php -v),您可以检查 the changelog for the bug number (69874) to see whether the fix 是否已应用于您的版本。

一个 short-term 修复是像这样替换对 mail() 的调用

 function fix_mail(  $to ,  $subject ,  $message , $additional_headers =NULL,  $additional_parameters=NULL ) {
            $to=filter_var($to, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES| FILTER_FLAG_STRIP_LOW| FILTER_FLAG_STRIP_HIGH);
            $subject=filter_var($subject, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES| FILTER_FLAG_STRIP_LOW| FILTER_FLAG_STRIP_HIGH);             

            if (!$additional_headers)
                    return mail(  $to ,  $subject ,  $message );

            if (!$additional_parameters)
                     return mail(  $to ,  $subject ,  $message , $additional_headers );

            return mail(  $to ,  $subject ,  $message , $additional_headers, $additional_parameters );
    }
上面的

None 为我修复了它 - 主要问题是你不能在 header 中放置 header 定义以外的任何内容。旧脚本会破坏其中的任何内容。因此,将塞入 header 的任何文本或附件移动到消息 body 中。有道理..

这有一个 explanation
(我想这与上面 Frank 的解决方案加上 Davisca 的解决方案相同 "no double new lines" - 但您 需要 将附件的新行加倍)

我的 PHP 版本 - 5.4.43, 可能包含修复错误 #68776。

谷歌搜索显示相同的错误 [http://fossies.org/diffs/php/5.4.42_vs_5.4.43/ext/standard/mail.c-diff.html]

=> 我不能使用空字符串作为 mail() 参数。

我的旧代码:

$headers = 'From: ' . $frm . "\r\n";
$headers .= 'To: ' . $contactEmail . "\r\n";
if ( $flag ) {
  $headers .= 'To: ' . $contactEmail2 . "\r\n";
}
$headers .= 'Cc: ' . $contactEmailCc . "\r\n";
$headers .= 'Bcc: ' . $contactEmailBcc . "\r\n";
$headers .= 'Return-Path: ' . $frm . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";

$headers .= $htmlText . "\r\n";

if (!mail('', $strSubject, '', $headers)) {    // !!! note the empty parameters.

我的新密码:

$headers = 'From: ' . $frm . "\r\n";
// note: no "To: " !!!
$headers .= 'Cc: ' . $contactEmailCc . "\r\n";
$headers .= 'Bcc: ' . $contactEmailBcc . "\r\n";
$headers .= 'Return-Path: ' . $frm . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";   
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
// note: no $htmlText !!!

// note: new parameters:  
$mTo = $contactEmail;
if ( $flag ) {
  $mTo .= ', ' . $contactEmail2;
}

$mMessage .= $htmlText . "\r\n";

if (!mail($mTo, $strSubject, $mMessage, $headers)) {

None 以上答案为我解决了这个问题。因此,我将搜索范围扩大到 "mail with attachment and HTML message issues." 将来自几个不同帖子的信息拼凑在一起,我想出了这个。它允许 BOTH HTML 电子邮件和附件。

我原来的header代码:

$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n";
$header .= $body."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n"; 
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n";
$header .= $content."\r\n";
$header .= "--".$uid."--";

if (mail($mail_to, $subject, "", $header))
{
    return "mail_success";
}
else
{
    return "mail_error";
}

我的新代码(完整): 请注意,$body 是由不同函数组装的 HTML。

$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);

$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

$eol = PHP_EOL;

// Basic headers
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

// Put everything else in $message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $body.$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";

if (mail($mail_to, $subject, $message, $header))
{
    return "mail_success";
}
else
{
    return "mail_error";
}

这里有两个关键变化。 (1) 将 header 中的所有 multi-part 内容删除到 $message 中。 (2) 删除所有“\r\n”内容并在代码中添加$eol = PHP_EOL;

总之,这些更改使我能够再次发送 HTML 带附件的电子邮件。

这将解决您的问题。我更改了 Frank 的一些代码。此代码将支持附件和 html.

 <?php

$filename  = "certificate.jpg";
$path      = "/home/omnibl/subdomains/test/certificate/certimage/";
$file      = $path . $filename;
$file_size = filesize($file);
$handle    = fopen($file, "r");
$content   = fread($handle, $file_size);
fclose($handle);

$content = chunk_split(base64_encode($content));
$uid     = md5(uniqid(time()));
$name    = basename($file);

$eol     = PHP_EOL;
$subject = "Mail Out Certificate";
$message = '<h1>Hi i m mashpy</h1>';

$from_name = "mail@example.com";
$from_mail = "mail@example.com";
$replyto   = "mail@example.com";
$mailto    = "mail@example.com";
$header    = "From: " . $from_name . " <" . $from_mail . ">\n";
$header .= "Reply-To: " . $replyto . "\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n";
$emessage = "--" . $uid . "\n";
$emessage .= "Content-type:text/html; charset=iso-8859-1\n";
$emessage .= "Content-Transfer-Encoding: 7bit\n\n";
$emessage .= $message . "\n\n";
$emessage .= "--" . $uid . "\n";
$emessage .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\n"; // use different content types here
$emessage .= "Content-Transfer-Encoding: base64\n";
$emessage .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\n\n";
$emessage .= $content . "\n\n";
$emessage .= "--" . $uid . "--";
mail($mailto, $subject, $emessage, $header);

如果这对任何人有帮助,我正在使用 PHP 5.6 和一个旧代码点火器 v1 电子邮件库

email.php:第 1510 行 - 我添加了这个:

     $this->_header_str = str_replace("\r\r","",$this->_header_str);
                    $this->_header_str = str_replace("\r[=10=]","",$this->_header_str);
                    $this->_header_str = str_replace("\r\n\r\n","",$this->_header_str);
                    $this->_header_str = str_replace("\n\n","",$this->_header_str);
                    $this->_header_str = str_replace("\n[=10=]","",$this->_header_str);

此行上方:

 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['Return-Path'])))
                            return FALSE;
                    else
                            return TRUE;

这就是成功清理电子邮件 headers 并解决了我收到的错误(与此问题的原始发布者相同)