johnwhitney
johnwhitney

Reputation: 93

Using exec() in PHP: Passing arguments

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.

Upvotes: 1

Views: 10472

Answers (2)

Luke
Luke

Reputation: 1870

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.

Upvotes: 1

Chris Hepner
Chris Hepner

Reputation: 1552

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'.

Upvotes: 4

Related Questions