doubu4406 2016-06-28 06:43
浏览 65
已采纳

PHP邮件换行问题与三元运算符在体内

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' . "
" .
        'This line also breaks properly' . "
" .
        'This one as well' . "

" .

        'This line ' . ( $value == true ? 'breaks' : 'does not break' ) . ' properly' . "
" .
        'This one works fine' . "
" .
        '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 , 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.

  • 写回答

1条回答 默认 最新

  • drhib62644 2016-06-28 09:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部