I need a bit of help understanding some PHP behavior that is not making sense to me, thanks in advance for the help.
First off, is there a difference between the use of \r\n
and \n\r
? I would think logically no, but I am thinking there has to be a practical reason why they are listed in the PHP document separately. I am currently using \r\n
but sometimes I get a line break, sometimes I don't, its befuddling.
Second, I am aware that if I was echoing the information inside the browser, I would use the nl2br()
function but I am not showing the information to the browser, it is being gathered and concatenated into a string that is then being sent via mail()
and that's where the visual discrepancy is showing up.
Here is the script, pretty straight forward, nothing mind boggling here:
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
$page = $_SERVER['PHP_SELF'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$from_page = $_SERVER['HTTP_REFERER'];
$time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']);
$uri = $_SERVER['SCRIPT_URI'];
$to = "***";
$subject = "Visitor Log";
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . "Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri;
$headers = "From: ***" . "\r\n" . "Reply-To: ***" . "\r\n" . "X-Mailer: PHP/" . phpversion();
mail($to,$subject,$message,$headers);
?>
But as you can see, I have linebreaks that are padded into the $message
for obvious readability of the email that I receive. But sometimes the emails arrive and everything is on a separate line as should be and sometimes it doesn't. So I thought maybe the method of adding a line break is not the best and that I am doing something wrong.
So the question is, what should I use to get the proper line breaks, if what I am using is not correct, and if it is correct, why isn't it always working? Your assistance to add some clarity to this would be greatly appreciated.
If it matters or makes a difference, I am using PHP 5 (5.5.28) Linux server with Apache.
Thank you for everyone's help but ultimately I ended up fixing it as follows and even added the content type for good measure (TY @TOM).
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
$page = $_SERVER['PHP_SELF'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$from_page = $_SERVER['HTTP_REFERER'];
$time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']);
$uri = $_SERVER['SCRIPT_URI'];
$to = "***";
$subject = "Visitor Log";
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )" . "\r\n";
$message .= "Browser : " . $browser . "\r\n";
$message .= "From : " . $from_page . "\r\n";
$message .= "Page : " . $page . "\r\n";
$message .= "Time : " . $time . "\r\n";
$message .= "Script : " . $uri;
$headers = "From: ***" . "\r\n";
$headers .= "Reply-To: ***" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "Content-Type: text/html; charset=\"utf-8\"" . "\r\n";
mail($to,$subject,nl2br($message),nl2br($headers));
?>
NOTE: The new formatting is not part of the solution, its just for myself. The solution is giving up on plain and going with html and using nl2br
function to force the breaks, not elegant, not what I wanted entirely but works.
Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.
The PHP concatenation operator (.) is used to combine two string values to create one string. .= Concatenation assignment.
We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify string in PHP. For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use double backslash (\\).
Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and & . Both carry out the basic concatenation operation, as the following example shows.
my best guess is that you sometimes send as text/plain and sometimes as text/html.. (since you do not specify anything, maybe the mail() or mailserver or client takes a best guess? you could investigate..)
i would try to force it to send as text/html and use
<br/>
's with nl2br, or force it as text/plain and use \n
like...
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . 'Content-Type: text/html; charset="utf-8"'."\r\n"."Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri;
..if that's how you force it to mail as html, im not sure about the header
The thing is that you are not passing the content-type
in the email header, so the email client could interpret it like plain-text
or text/html
depending of the vendor. The best is to pass the content-type
as parameter in the email header.
content-type:text/plain
: \n
as line breakcontent-type:text/html
: </br>
as line breakIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With