Jump to content
  • Advertisement
Sign in to follow this  
benryves

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

This topic is 4107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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.

Share this post


Link to post
Share on other sites
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!