1
votes

I've set up a local dev environment on snow leopard, and have set postfix up to send email via my isp mail server.

I eventually got postfix to work after much frustration, but now when my emails send the header information is bunged up!

I'm using the following php code:

$email = "me@mydomain";
$subject = "Email tester";
$body = "Simple test";
$header = "From: me@mydomain \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);

echo "message sent!";

The To: and Subject: headers display as they should, in the header!, but the rest display in the email body. This makes the email look like the from field in email client is empty.

I've tried a variety of php scripts, some very simple, but its the same thing, headers always displaying in the email body.

I'm thinking it could be a postfix problem, but not sure, anyone encountered this type of problem before?

3
I've tried <pre>\n</pre> line endings to no avail :(Alastair Hodgson

3 Answers

4
votes

Use PHP_EOL instead of \r\n in *additional_headers*, i.e. $header in your example. PHP_EOL will substitute the newline correspondingly to the OS you are running on.

Also, message should contain LN only i.e. \n. This is accordingly to PHP documentation. Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.

Make sure you meet both of criterias in your script - I've tried to achieve it and finally got it working with the default configuration of Postfix.

3
votes

This is almost 100% not a Postfix problem, but something caused by your code. The body starts once a blank CRLF is seen after the headers.

You should dump out your email body text and see if you're not accidentally introducing an extra CRLF.

0
votes

Investigating this problem further (basically because I didn't want to improve lots of scripts just because of that), I've come to the point that there is a strong conflict between PHP and Postfix developers, which is not fixed until now. You can read wide info here:

http://www.mail-archive.com/[email protected]/msg03226.html

Postfix expects EOL to be LF when picking up mail from sendmail on unix and replaces that with CRLF when sending. When it gets CRLF it still replaces the LF and we get CRCRLF.

It explains broken headers. To solve this, you must exactly know how your Postfix and PHP/mail system works. If you experience problems like described above, just use "\n" instead of "\r\n". If you program a complicated system which could run on both Windows/Unix, just introduce an extra param like $eeol="\r\n"; which will be put instead of direct "\r\n" tag and that way could be easily configured for any system.

I suppose that's the approach Postfix author recommends:

It would be really good if PHP application programmers formatted email messages in a consistent manner. For example, they could use a variable that contains the END-OF-LINE terminator, instead of hard-coding LF or CRLF line terminators all over the place.