PHP Mail - Multiple or malformed newlines found

a 夏天 提交于 2020-01-03 17:42:28

问题


we upgraded our version of PHP and are now getting the error " Warning: mail(): Multiple or malformed newlines found in additional_header".

I've created experimenting with different things but haven't gotten anything to work. I apologize as I'm not very familiar with how all of this works, so please bear with me.

The objective (which worked in earlier versions) is to send an HTML based message (has ,
tags, etc) that includes a PDF attachment that exists on our server.

If you can give me specific adjustments, I'd appreciate it greatly!

$sFrom = "[Company Name] <[Our Email]>";
$sReplyTo = "[Our Email]";
$sParams = "-f [Our Email]";
$attachment = chunk_split(base64_encode(file_get_contents($sPath)));
$uid = md5(uniqid(time()));

$sHeaders = "From: ".$sFrom."\n".
            "Reply-To: ".$sReplyTo."\n".
            "MIME-Version: 1.0\n".
            "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n".
            "This is a multi-part message in MIME format.\n".
            "--".$uid."\n".
            "Content-Type: text/html; charset='iso-8859-1'\n".
            "Content-Transfer-Encoding: 7bit\n\n".
            $sMessage."\n\n".
            "--".$uid."\n".
            "Content-Type: application/pdf; name=\"".$sFileName."\"\n".
            "Content-Transfer-Encoding: base64\n".
            "Content-Disposition: attachment; filename=\"".$sFileName."\"\n\n".
            $attachment."\n\n".
            "--".$uid."--";    
if (!mail($sTo, $sSubject, "", $sHeaders, $sParams)) {
    $bError = true;
}

回答1:


because of https://bugs.php.net/bug.php?id=68776 multiple linebreaks are not allowed anymore (or at the moment?). Try to switch to PEAR Mailer, PHPMailer or something else.

Multiple linebreaks "\n\n" are needed to send php mails with attachments with mail()...




回答2:


Try Using \\\n instead of \n.




回答3:


1) You will get an error for trying to append to the uninitialized variable $sMessage. Also check that the other variables have the expected content ($sTo, $sSubject).

2) You moved your message content from header into message but forgot to add it to the mail function if (!mail($sTo, $sSubject, "", $sHeaders, $sParams)) will become if (!mail($sTo, $sSubject, $sMessage, $sHeaders, $sParams))

3) You still have multiple newlines after "multipart/mixed"

4) Delete the lineof "This is a multi-part message in MIME format" Here I updated your code:

$sFrom = "me@aju.ro";
$sReplyTo = "me@aju.ro";
$sParams = "-f me@aju.ro";
$attachment = chunk_split(base64_encode(file_get_contents($sPath)));
$uid = md5(uniqid(time()));

$sHeaders = "From: ".$sFrom."\n".
            "Reply-To: ".$sReplyTo."\n".
            "MIME-Version: 1.0\n".
            "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n".
            "--".$uid."\n".
            "Content-Type: text/html; charset='iso-8859-1'\n".
            "Content-Transfer-Encoding: 7bit\n\n".
$sMessage="\n\n".
            "--".$uid."\n".
            "Content-Type: application/pdf; name=\"".$sFileName."\"\n".
            "Content-Transfer-Encoding: base64\n".
            "Content-Disposition: attachment; filename=\"".$sFileName."\"\n\n".
            $attachment."\n\n".
            "--".$uid."--";    
if (!mail($sReplyTo, $sSubject, $sMessage, $sHeaders, $sParams)) {
    $bError = true;
}



回答4:


This is the php up gradation change.A security risk in PHP mail() function has been fixed and extra newlines are allowed no more.

Please remove multiple newlines in additional_headers argument. These count as "multiple or malformed newlines": \r\r, \r\0, \r\n\r\n, \n\n, \n\0.



来源:https://stackoverflow.com/questions/30942998/php-mail-multiple-or-malformed-newlines-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!