dongmi5177 2018-10-25 19:40
浏览 120
已采纳

在php中的exec中添加echo中间的新行

Here is my exec command for sending email to users. I want to reformat the body of the mail by adding a newline after each App, config and counter type.

Here is the code that works but outputs everything in one line:

exec('echo "Please find attached the query result for:  App: '.$varApp.'  Config: '.$varConfig.' Counter Type: '.$varCtrType.'  Thanks!" | /bin/mail -s "Testing" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 );

Here is the output from that command in the user email body:

Please find attached the query result for:  App: ABCDEF Config: GHIJK   
Counter Type: LMNOP <br> Thanks!

I have tried using
and /r/n without any luck. I also tried different combinations of adding quotes and /.

The code that doesn't give any desired output.

exec('echo "Please find attached the query result for:'<br>'  App: '.$varApp.' '<br>' Config: '.$varConfig.' <br> Counter Type: '.$varCtrType.' <br> Thanks!" | /bin/mail -s "Testing" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 )

OR

exec('echo "Please find attached the query result for:/r/n  App: '.$varApp.' /r/n Config: '.$varConfig.' <br> Counter Type: '.$varCtrType.' <br> Thanks!" | /bin/mail -s "Testing" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 );

I think I don't know how to add breaks/newline in middle of this echo statement. Any guidance here is deeply appreciated!

Thanks!

  • 写回答

1条回答 默认 最新

  • dongxin8392 2018-10-25 20:14
    关注

    Use the printf command instead of echo, it processes escape sequences in the format string.

    Also, the escape sequence for newline is , not /n. You don't need at all.

    exec('printf "Please find attached the query result for:
      App: %s 
     Config: %s 
     Counter Type: %s 
     Thanks!" "' . $varApp . '" "' . $varConfig . '" "' . $varCtrType . '"| /bin/mail -s "Testing" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?