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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: