8

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.


UPDATED ANSWER

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.

1
  • 1
    It is never appropriate to have an "updated answer" in the question body. Commented Apr 1, 2024 at 1:37

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I think you might be onto something as Tom suggested the same thing. The problem I am having is that I have added the content type now and still not having an effect. Thinking I might have missed something.
1

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 break
  • content-type:text/html: </br> as line break

6 Comments

aha, I was thinking about that but didn't cross my mind that it would be critical since it defaults to text/plain but let me give that a shot and get back to you, thank you.
I did as you suggested and unless I messed it up, its not having any effect. I changed it to $headers = "From: ***" . "\r\n" . "Reply-To: ***" . "\r\n" . "content-type: text/plain" . "\r\n" . "X-Mailer: PHP/" . phpversion(); and still getting a blob, no line breaks. Did I miss something?
@GµårÐïåñ: add it like $headers .= 'Content-Type: text/plain; charset=\"utf-8\"\r\n";
Thank you for that, I will give that a try but to make sure, was the switch to /html accidental or on purpose, given that we want to make sure the \n get processed and that was supposed to be /plain, just curious. Also what's your thought on using PHP_EOL would that help in this situation?
Ok, tried it with your suggestion, both as written with the /html and also with /plain, made no difference, still a couple of lines are newlined and a couple of them are not, so frustrating.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.