在 PHP 中使用 exec():传递参数

发布于 2024-11-27 12:09:26 字数 858 浏览 1 评论 0原文

出于测试目的,假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2024-12-04 12:09:26

如果没有错误信息,我不确定这是否是整个问题,但我将从以下开始:

$argv 读取的参数是空格分隔的。以下代码:

 /usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP $TO $SUB $BODY $FROM > /dev/null &

在您的示例中执行如下:

 /usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP [email protected] Yadda This is a test [email protected] > /dev/null &

这使得 $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:

 /usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP $TO $SUB $BODY $FROM > /dev/null &

is executing as follows in your example:

 /usr/bin/php /xxx.yyy.net/TESTS/sendit.PHP [email protected] Yadda This is a test [email protected] > /dev/null &

That makes $argv[3] == 'This' and $argv[4] == 'is'.

旧竹 2024-12-04 12:09:26

如果没有任何调试,我认为没有人能够给你答案。

您必须记住*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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文