PHP_EOL works for what it's supposed to do, but "work" is one of those generic verbs that doesn't actually say much by itself. PHP_EOL is the
newline for the PHP interpreter's platform. It's useful to output a newline to suit the interpreter's platform, rather than the platform the script was written on.
Under certain circumstances, compilers translate "\n" to "\x0D\x0A". The PHP interpreter will inherit this, as it uses escape characters rather than literals in its scanner. This is the situation where '
PHP_EOL == "\n"' I referred to. This actually doesn't always hold, even on platforms that use CRLF for newlines.
My previous code is wrong in that it targets the interpreter platform, where it should target the authoring platform. Changing "\n" to a literal newline will fix this. We could also add code to support platforms that use line feeds as newlines, though there aren't enough in use to make it very worthwhile.
PHP:
<?php
switch ("
") {
case "\x0D\x0A":
$sol = $eol = '';
break;
case "\x0A":
$sol='';
$eol = "\r";
break;
case "\x0D":
$sol="\x0A";
$eol = '';
break;
}
...
Messy, messy.
Alternatively, it could use implode rather than multiline strings.
PHP:
<?php
define('CRLF', "\x0D\x0A");
$to = "maulikan@agmr.x10.bz";
$subject = "Send test";
$mailheaders = implode(CRLF, array(
"From: Alberto Marambio <>",
"Reply-To: $sender_email",
""
));
$msg = implode(CRLF, array(
"ESTOS SON LOS DATOS RECIBIDOS:",
"Nombre: $_POST[sender_name]",
"E-Mail: $_POST[sender_email]",
"Mensaje: $_POST[message]",
""
));
...
Or there's that preg_replace option.
PHP:
define('CRLF', "\x0D\x0A");
function h2n_nl($str) {
return preg_replace("/(?<!\x0D)\x0A/", CRLF, $str);
}
$to = "maulikan@agmr.x10.bz";
$subject = "Send test";
$mailheaders =<<<EOS
From: Alberto Marambio <>
Reply-To: $sender_email
EOS;
$msg =<<<EOS
ESTOS SON LOS DATOS RECIBIDOS:
Nombre: $_POST[sender_name]
E-Mail: $_POST[sender_email]
Mensaje: $_POST[message]
EOS;
$msg = h2n_nl($msg);
$mailheaders = h2n_nl($mailheaders);
...
If the mailer is configured properly, I believe it will translate line feeds into CRLFs, so the above is only necessary if the mailer is misconfigured.