doubu4406 2016-06-28 14: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 17: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.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?