【问题标题】:PHP shell_exec failed due to some specific characterPHP shell_exec 由于某些特定字符而失败
【发布时间】:2017-04-20 07:44:27
【问题描述】:

我在Ubuntu上使用shell_exec函数调用mailx命令发送邮件,但是由于命令中有特定字符而收到错误,请参阅我下面的代码和结果。

<?php
$stringA="Visit STAR WARS™";
$stringB="Visit STAR WARS";

echo "StringA:", $stringA, "<br/>";
$res = shell_exec("echo $stringA | mailx -s 'testA' my_email_address@mail.com");
echo $res, "<br/>";

echo "StringB:", $stringB, "<br/>";
$res = shell_exec("echo $stringB | mailx -s 'testB' my_email_address@mail.com");
echo $res, "<br/>";

当我在服务器上调用这个页面时,我得到了以下页面:

StringA: Visit STAR WARS™
"/var/www/dead.letter" 1/19 
StringB: Visit STAR WARS

我收到了内容为Visit STAR WARS的邮件,却收不到内容为Visit STAR WARS™的邮件。

我从 phpinfo 函数查到,default_charset 是“utf-8”。 如果我直接在 Ubuntu shell 上运行命令"echo Visit STAR WARS™ | mailx -s 'testB' my_email_address@mail.com",我可以得到带有**™**内容的邮件。

谁能帮我解决这个问题,以便 shell_exec 函数可以发送带有特定字符的邮件。

【问题讨论】:

  • shell_exec("echo -n \"$stringA\"| mailx -s 'testA' my_email_address@mail.com");
  • 使用escapeshellarg 转义你的特殊字符
  • @Cyclonecode 我测试过,但问题依旧。
  • @hassan, $stringA=escapeshellarg("Visit STAR WARS™"); $res = shell_exec("echo -n $stringA | mailx -s 'test' tiancheng.li@sony.com"); 帮不了我,问题仍然存在。
  • shell_exec 的返回值是什么?检查标准错误(通过将标准错误重定向到标准输出)

标签: php shell-exec mailx


【解决方案1】:

$[美元符号]在shell脚本中有特殊含义,不能在命令中传递,

$res = shell_exec("echo '" . $stringA . "' | mailx -s 'testA' my_email_address@mail.com");

最好使用escapeshellarg 来转义您的论点,如下所示:

$res = shell_exec("echo '" . escapeshellarg($stringA) . "' | mailx -s 'testA' my_email_address@mail.com");

【讨论】:

  • 有点奇怪。如果我在变量定义中添加了 escapeshellarg,错误就消失了。 $stringA=escapeshellarg ("Visit STAR WARS™"); ,如果不是($stringA="Visit STAR WARS™"; ),问题依然存在。但是,我无法收到邮件以为错误消失了。
  • 那么你需要跟踪你的mailx执行
  • 谢谢,我通过在mailx 上设置字符集解决了这个问题。 mailx -S ttycharset=utf-8 -S sendcharsets=utf-8 ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2013-06-17
  • 1970-01-01
  • 2012-05-25
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多