在 PHP 中使用 exec():传递参数
出于测试目的,假设 input.PHP 如下所示
<?php
{
$TO = "[email protected]";
$FROM = "[email protected]";
$SUB = "Yadda";
$BODY = "This is a test";
exec("/usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP $TO $SUB $BODY $FROM > /dev/null &");
echo "DONE";
}
?>
由 exec() 调用的 sendit.PHP 如下所示
<?php
$to = $argv[1];
$subject = $argv[2];
$message = $argv[3];
$headers = 'From: '.$argv[4]. "\r\n" .
'Reply-To: '.$argv[4]. "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
当我在浏览器中打开 input.PHP 时,我收到 echo DONE 消息,但测试电子邮件不是发送。我的代码有什么问题吗?谢谢。
For testing purposes, let's say input.PHP looks like this
<?php
{
$TO = "[email protected]";
$FROM = "[email protected]";
$SUB = "Yadda";
$BODY = "This is a test";
exec("/usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP $TO $SUB $BODY $FROM > /dev/null &");
echo "DONE";
}
?>
And the sendit.PHP which is called by exec() looks like this
<?php
$to = $argv[1];
$subject = $argv[2];
$message = $argv[3];
$headers = 'From: '.$argv[4]. "\r\n" .
'Reply-To: '.$argv[4]. "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
When I open input.PHP in my browser, I get the echo DONE message, but the test email is not sent. What's wrong with my code? Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有错误信息,我不确定这是否是整个问题,但我将从以下开始:
$argv
读取的参数是空格分隔的。以下代码:在您的示例中执行如下:
这使得
$argv[3] == 'This'
和$argv[4] == 'is'
。Without error information, I'm not sure if this is the entire problem, but I'd start with this:
Arguments as read by
$argv
are space-delimited. The following code:is executing as follows in your example:
That makes
$argv[3] == 'This'
and$argv[4] == 'is'
.如果没有任何调试,我认为没有人能够给你答案。
您必须记住*nix 区分大小写。因此,您必须确保 /xxx.yyy.net/TESTS 等实际上大小写正确,拼写正确。
另外,我建议不要将所有内容发送到
/dev/null
甚至可能发送到文件。只是因为/usr/bin/php
可能使用不同的配置(以前发生在我身上),并且当我在 crontab 中运行脚本时它没有按预期工作。您需要了解更多信息!检查 php 日志,看看当您从终端运行脚本时该脚本会提供什么。
Without any debugging I don't think anyone will be able to give you an answer.
You have to remember that *nix is case sensitive. So you have to make sure that /xxx.yyy.net/TESTS etc are actually in correct case, spelled correctly.
Also I would suggest not sending everything to
/dev/null
and maybe to a file. Simply because/usr/bin/php
could be using different config (happened to me before) and it didnt work as espected when I ran scripts in crontab.You need to find out some more info! Check php logs, see what that script gives you when you run it from terminal.