Using exec() in PHP: Passing arguments

夙愿已清 提交于 2019-12-11 07:25:36

问题


For testing purposes, let's say input.PHP looks like this

<?php
{
$TO = "joe@net.net";
$FROM = "bob@net.net";
$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.


回答1:


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 joe@net.net Yadda This is a test bob@net.net > /dev/null &

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




回答2:


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.



来源:https://stackoverflow.com/questions/6903691/using-exec-in-php-passing-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!