Sign in to follow this  
benryves

[web] [PHP4] mail() - empty lines vanishing

Recommended Posts

benryves    1999
Further mail woes; I'm attempting to send out multipart messages (one part plaintext, one part HTML). I send an empty message body, building up the message contents manually and sending the entire thing via the additional headers parameter for mail(). I'm using Apache2/IIS for testing purposes. I can check what would be sent by looking at the .eml files in C:\Inetpub\mailroot\Queue\. Empty lines are important parts of the message, as you'd imagine. However, the emails are displayed as blank, and opening up the .eml in a text editor shows that the empty lines have been stripped out of the message. Manually re-inserting them fixes the message. Is this an IIS or PHP issue? The empty lines are in the headers before being sent. For the record, I currently use this to send out HTML mail;
$headers = array();

$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'Content-Transfer-Encoding: base64';
$headers[] = 'To: ' . $to;
$headers[] = 'From: ' . $from;

$headers = implode("\r\n", $headers);
	
$body = chunk_split(base64_encode($body));

mail($to, $subject, $body, $headers);
This works, but I'd also like a plaintext version sent out with it.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Blank lines in the headers? In an e-mail, as far as I know, the headers are separated from the message body by a blank line ("\r\n"), so the first blank line in the message is taken to mark the end of the headers.

Share this post


Link to post
Share on other sites
Sander    1332
Yeah, you're missing the \r\n at the end of the last header line. The implode() only puts \r\n between the header lines, not at the end. mail() adds one \r\n between the headers and the body. So, you get this:


header-1\r\n
header-2\r\n
header-3\r\n <-- that \r\n is from the mail() command
body


What you need:


header-1\r\n
header-2\r\n
header-3\r\n
\r\n <-- that \r\n is from the mail() command
body


Your fix:


$headers = implode("\r\n", $headers) . "\r\n";


Share this post


Link to post
Share on other sites
Coward    210
Also, if you want to send mails to gmail, using "\r\n" in the header is not the way to go. For some reason "\r\n" makes gmail end the header parsing and starts printing the rest of the header as if it was a part of the mailbody..

The only solution I've seen to this problem is to simply send a \n instead.

I've been browsing through the RFC822 standards looking for an explanation for this, and it simply seems to me that gmail got it wrong. I'm not realy used to reading standard documents, so maybe I'm misunderstanding something completely...

Anyone know anything deeper about this issue?

Share this post


Link to post
Share on other sites
benryves    1999
Quote:
Original post by Sander
Yeah, you're missing the \r\n at the end of the last header line. The implode() only puts \r\n between the header lines, not at the end. mail() adds one \r\n between the headers and the body.
Ah, interesting, though that code snippet I posted does actually appear to work (the extra blank line is being inserted by something else, it would appear, as it's in the final email source).

The specific problem is that if I hand-craft a multi-part message, I store the entire thing - headers and all - in a single string and pass that to the mail function as the additional headers parameter, with an empty string for the message parameter, like this site demonstrates. When I look at the resulting .eml to be sent out by IIS, the empty lines (eg, the empty lines directly and above "This is a MIME encoded message.") have been stripped out.

@Coward: It appears some mail servers don't like \r\n either, for some reason. I'm sticking to it for the moment as it's "proper".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this