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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真