4

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;
}
user1344849
  • 41
  • 1
  • 1
  • 2
  • 1
    Possible duplicate of [Error with PHP mail(): Multiple or malformed newlines found in additional\_header](http://stackoverflow.com/questions/30887610/error-with-php-mail-multiple-or-malformed-newlines-found-in-additional-header) – Ankur Feb 05 '17 at 14:39

5 Answers5

2

Try Using \\\n instead of \n.

Nikhil
  • 3,711
  • 8
  • 32
  • 43
0

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

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;
}
profimedica
  • 2,716
  • 31
  • 41
0

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.

ubm
  • 636
  • 11
  • 21
0

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

Community
  • 1
  • 1
Thomas
  • 1,058
  • 8
  • 15