Page 1 of 1

Trying to use exec with mail

Posted: Mon Oct 27, 2008 9:59 pm
by djepayne
My first post here.

I want to use test.php to run a php script in the background:

Code: Select all

 <?php
exec('/usr/bin/php /pathname/test_exec.php > /dev/null 2>&1 &');
?> 
Here's the test_exec.php file:

Code: Select all

 #!/usr/bin/php -q
<?php
mail("to@mail.com","subject","message","From: me@mail.com");
?> 
The problem is that mail doesn't execute, and I get no error messages.

I've also redirected the exec output to a text file, and still no error messages or clues as to why this isn't working.

I can do simple statements like:

Code: Select all

exec('ls > results.txt')
Eventually I want to use this technique to send a email messages to a list of people in my association who have opted in. My present site is just using the "mail" command inside of a foreach statement and that causes my web page to stay open for several minutes. This background exec seemed like a more elegant approach. I'm hosting with http://www.1and1.com on a Linux server.

Any help or clues are appreciated

Re: Trying to use exec with mail

Posted: Tue Oct 28, 2008 6:50 am
by Kadanis
Not 100% but I think its the > /dev/null part of your exec command. I've done something similar in the past and didn't include that. As far as I can recall, redirecting the output like that basically dumps it into a black hole and you end up with just a listener. It may be affecting the mail function, but more importantly won't be writing the error messages if there are any.

Re: Trying to use exec with mail

Posted: Tue Oct 28, 2008 9:56 am
by djepayne
Good point, however I tried it without redirecting the output and still no go.

Last night I switched to using the simple Unix "sendmail" command and started getting results:

Code: Select all

 
exec("cat test.txt | /usr/sbin/sendmail -i me@mail.com &");