PHP Mail - Multiple or malformed newlines found

11.8k views Asked by At

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;
}
5

There are 5 answers

0
Kewal Krishan Bindal On

Try Using \\\n instead of \n.

0
profimedica On

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 = "[email protected]";
$sReplyTo = "[email protected]";
$sParams = "-f [email protected]";
$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;
}
0
Lennart Schreiber On

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()...

0
ubm On

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.

0
Thomas On

In SMTP standard https://www.rfc-editor.org/rfc/rfc2045#section-2.1 the newline is defines as \r\n