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";

I know it seems trivial but it’s one of those little things that can result in a fresh bald spot on your head if it happens to you. Hopefully this will leave the balding up to Mother Nature again.

Related Posts:

Tags: ,

September 9th, 2008 at 2:41 pm | Posted in Programming
BlinkList del.icio.us digg Furl linkaGoGo Newsvine reddit Shadows Simpy Shinn Spurl.net Stumbleupon Tailrank Yahoo! My Web

4 Responses to “PHP mail() Headers Are Displayed In Email Body”

  1. Jacob Anders Says:

    Can I have more details on this?

  2. Jamie Thompson Says:

    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? ;-)

  3. Mike Says:

    @ 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!

  4. Digitalchaos Says:

    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.

Leave a Reply