PHP mail() Headers Are Displayed In Email Body

Here’s a little gotcha that may rear its head one day and hopefully this will help prevent you from going nuts over such a little problem. If you are using PHP to send email from your web application and are using the built in mail() function and see your custom headers being displayed in your email body you’ve got a syntax error that’s not directly in your PHP. This can make it hard to diagnose and fix.

Take this code sample:

// Set our parameters
$to       = 'john@fakedomain.com';
$subject  = 'This is a sample email';
$message  = 'This is a test of the emergency broadcast system. This is only a test';
$headers  = 'Reply To: john@fakedomain.net' . "\r\n";
$headers .= 'From: john@fakedomain.net' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
 
// Send the email
mail($to, $subject, $message, $headers);

If you were to use run this code your email would look like this:

Reply To: John Conde
From: John Conde
X-Mailer: PHP/5.2.5

This is a test of the emergency broadcast system. This is only a test

Can you spot the problem? It’s hard to spot at a glance and if you are unfamiliar with the proper format for email headers (see RFC 2822) it’s easy to miss. The problem lies with our ‘Reply To’ header. We’re missing the dash bewteen the two words which cause the headers to end prematurely. Subsequently all following headers, including the malformed header, are included in the body of the email message.

Fixing it as easy as all you have to do is correct that minor mistake.

$headers  = 'Reply-To: john@fakedomain.net' . "\r\n";

10 thoughts on “PHP mail() Headers Are Displayed In Email Body

  1. That’s not the only problem. Using \r\n as a line ending will cause many mail clients to incorrectly parse the header. \n seems to be a more universally accepted solution. Isn’t email fun? 😉

  2. @ Jamie Thompson

    Thanks for your comment. You helped me sort out a long standing issue I’ve had.

    The weird thing is that when the test script was run from our development environment, we did not see the excess headers in the email body. But when we ran the same script in our production environment, it put the headers in the body every time. Removing the carriage return fixed it. I’m not sure whether to blame our production host, our mail server, or both!

  3. Thanks for this post. I’ve had headers creeping into the body of the message.
    Jamie Thompson’s note solved the problem I’ve been struggling with for months. The switch from \r\n to just \n fixed it in the applications we are using.

  4. YES!
    Thanks Jamie, changing \r\n to \n solved my problem.
    This has been bugging me for a while.
    Thank god for this website!!!! :?)

  5. when I send mail from my site, no problem with $to but when i use CC
    header are displayed in email body
    Content-Type: text/html; charset=utf-8
    and HTML code also like
    what i do

  6. Nice tip. Thanks!

    If your script don’t send e-mails via PHP mail() function, see your server specification for REQUIRED headers.

    For the problem with the header at the e-mail’s body, see if your server uses \n (usually linux servers) or \r\n (usually windows servers), or check syntax errors at the header notation.

  7. Hi John, Your post, even after so many years, helped me so much. I was literally going nuts as the client has already gone live and the emails doesn’t work in entourage. The header X-PHP-Originating-Script implanted by PHP was causing all the trouble. Thanks.

  8. can I just say AAARRRGGHHH! Now thank you for enlightening me about \r\n vs \n cause wow I’d never had gotten that and would likely have ended up in Arkham Asylum or something.

Leave a Reply

Your email address will not be published. Required fields are marked *