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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?