PHP mail()出错:在additional_header中找到多个或格式错误的换行符

时间:2015-06-17 09:31:01

标签: php apache sendmail

突然开始收到上述错误而未对脚本进行任何更改。

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

该脚本在不同的服务器上仍然可以正常工作,所以我怀疑是否必须有一些服务器配置更改导致这一点,尽管主机恳求无知。

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

10 个答案:

答案 0 :(得分:57)

只是有类似的问题。
它突然出现了。没有更改PHP代码。

改变了什么:PHP升级5.5.25-1到5.5.26。

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

以前的工作正常,例如只是在标题之后添加额外的换行符或甚至将整个消息传递给additional_headers,将不再起作用。

解决方案

  • 清理标题。 additional_headers参数中没有多个换行符。这些被视为“多个或格式错误的换行符”:\r\r, \r\0, \r\n\r\n, \n\n, \n\0
  • 仅对标题使用additional_headers。电子邮件消息(多部分或不具有ir,没有附件等)属于message参数,而不是标题。

PHP安全漏洞报告:https://bugs.php.net/bug.php?id=68776
C Code diff如何修复:http://git.php.net/?p=php-src.git;a=blobdiff;f=ext/standard/mail.c;h=448013a472a3466245e64b1cb37a9d1b0f7c007e;hp=1ebc8fecb7ef4c266a341cdc701f0686d6482242;hb=9d168b863e007c4e15ebe4d2eecabdf8b0582e30;hpb=eee8b6c33fc968ef8c496db8fb54e8c9d9d5a8f9

答案 1 :(得分:40)

上述答案都没有为我解决这个问题。因此,我将搜索范围扩展到带有附件和HTML消息问题的邮件。"从几个不同的帖子拼凑信息,我想出了这个。它允许 BOTH HTML电子邮件和附件。

我的原始标题代码:

$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)从标题中删除所有多部分内容到$ message。 (2)删除所有&#34; \ r \ n&#34;东西并将$eol = PHP_EOL;添加到代码中。

这些更改一起让我再次发送带附件的HTML电子邮件。

答案 2 :(得分:11)

有同样的问题: 从标题中删除了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);

答案 3 :(得分:5)

以上都没有为我修复它 - 主要问题是你不能在标题中添加除标题定义以外的任何内容。旧脚本在那里包含任何东西。因此,将填充到标题中的任何文本或附件移动到邮件正文中。有道理..

这有explanation
(我想它与弗兰克上面的解决方案相同,加上戴维斯卡&#34;没有双重新线&#34; - 但你需要加倍新的附件行)

答案 4 :(得分:2)

另一种带来相同新错误的方案是,如果您没有向&#34; mail&#34;发送任何标题。命令。它过去只使用默认值,现在会产生误导性错误:&#34;在additional_header&#34;中找到多个或格式错误的换行符。

可以通过添加:

来修复
y

答案 5 :(得分:2)

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

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

=&GT;我不能将空字符串用作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)) {

答案 6 :(得分:2)

这将解决您的问题。我改变了一点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);

答案 7 :(得分:1)

如果您没有做任何愚蠢的事情(即忘记清理标题),您可能会遇到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以获取错误号(69874),以查看是否已为您的版本应用了the fix

短期解决方法是替换对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 );
    }

答案 8 :(得分:0)

很可能有人试图利用您的代码来注入电子邮件标题。

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

我建议您检查访问日志等并查找异常活动。您收到错误消息的事实有望意味着您的脚本没有受到损害,而是错误输出。你需要确定。

答案 9 :(得分:0)

以防万一,我使用的是PHP 5.6和旧的代码点火器v1电子邮件库

email.php:第1510行-我添加了此内容:

     $this->_header_str = str_replace("\r\r","",$this->_header_str);
                    $this->_header_str = str_replace("\r\0","",$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\0","",$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;

这成功地清除了电子邮件标题并解决了我收到的错误(与该问题的原始发布者一样)