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

Started by
3 comments, last by benryves 17 years ago
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Advertisement
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.
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\nheader-2\r\nheader-3\r\n     <-- that \r\n is from the mail() commandbody


What you need:

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


Your fix:

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


<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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?
Don't Temp Fate..
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".

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement