1

I'm experiencing a weird problem with PHP's mail function that I just can't figure out. Each line in the email body is separated by a new line or two but if I use a ternary operator on one of the lines, the line does not break. The code looks something like this:

$body = 'This line breaks properly' . "\r\n" .
        'This line also breaks properly' . "\r\n" .
        'This one as well' . "\r\n\r\n" .

        'This line ' . ( $value == true ? 'breaks' : 'does not break' ) . ' properly' . "\r\n" .
        'This one works fine' . "\r\n" .
        'New line again';

mail( 'me@example.com', 'Test Email', $body, $headers );

The output I get is:

This line breaks properly
This line also breaks properly
This one as well

This line does not break properly This one works fine
New line again

I've double and triple checked to make sure I put double quotes around each \r\n, I tried ( $value == true ) within the ternary, and I've tried setting Content-Type: text/plain; in the header string. It only works when I completely removed the ternary operator. I'm using PHP 5.5.36 if that has any relevance to what's going on.

mcon
  • 679
  • 6
  • 22
  • @JoseManuelAbarcaRodríguez I tried that one as well but I forgot to mention it. – mcon Jun 28 '16 at 14:48
  • @JoseManuelAbarcaRodríguez Nope. The line is still not breaking. – mcon Jun 28 '16 at 15:01
  • Try your code in http://phpfiddle.org/, it works to me, so, it's you. – Jose Manuel Abarca Rodríguez Jun 28 '16 at 15:07
  • why not wrap all your whole string blocks in double quotes instead of concatenating strings with single and then double quotes, that's just asking for confusion. Why use single quotes at all when you know double quotes are meant to be used for some aspects of the string content? – Martin Jun 28 '16 at 15:22
  • http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Martin Jun 28 '16 at 15:25
  • I have tested your issue and found that in all instances the output is correct and as intended. You have a specific issue in your system setup, or some other part of your code you have not stated here. – Martin Jun 28 '16 at 15:32

1 Answers1

0

I figured out what it was. Or rather, someone else figured out what it was. It's an issue with Outlook automatically removing "extra" line breaks from plaintext emails. I found the answer on this question and the 40 character/spaces issue was the exact behavior I was experiencing as well.

As a fix, I decided to just send HTML mail instead because <br> tags are easier to work with I guess.

Community
  • 1
  • 1
mcon
  • 679
  • 6
  • 22