Search code examples
phpemailcronssmtp

php mail script not working with cron


I have a problem getting a cron job to run (properly).

I created a (very large, but whittled down to nothing) php page that calls mail()

<?
    mail('[email protected]', 'subject', 'test');
    mail('[email protected]', 'subject', 'test');
?>

Then I created my cron job, which runs the file. The line of code in crontab -e is as follows:

12 0 * * * /opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/cron.php

If I run the command /opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/cron.php from terminal, I get an email sent to myself. However, if I run the same line from a cron job, it does not work.

My next stop was to check the logs. I'm running Ubuntu with sSMTP.

Apr 16 11:49:17 drew-Virtual crontab[4722]: (drew) END EDIT (drew) //EDITED CRON

//Calling cron.php file from terminal
Apr 16 11:49:31 drew-Virtual sSMTP[4791]: Creating SSL connection to host
Apr 16 11:49:32 drew-Virtual sSMTP[4791]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:49:34 drew-Virtual sSMTP[4791]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=444
Apr 16 11:49:34 drew-Virtual sSMTP[4794]: Creating SSL connection to host
Apr 16 11:49:35 drew-Virtual sSMTP[4794]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:49:37 drew-Virtual sSMTP[4794]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=454
//I successfully received 2 emails, one to my work account, one to my personal account

//Calling cron.php from cron
Apr 16 11:50:01 drew-Virtual cron[857]: (drew) RELOAD (crontabs/drew)
Apr 16 11:51:01 drew-Virtual CRON[4808]: (drew) CMD (/opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/chron.php)
Apr 16 11:51:01 drew-Virtual sSMTP[4810]: Creating SSL connection to host
Apr 16 11:51:02 drew-Virtual sSMTP[4810]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:51:04 drew-Virtual sSMTP[4810]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=698
//I did not receive any emails

You can see it only tries to send 1 email, and I assume fails as I never receive it. However, it doesn't tell me why it failed, nor do I have any other clues. I also noticed that the outbytes is larger in the cron job than it is from the command line.

Lastly, the php file has full rwx permissions, for everyone.


Solution

  • The issue was that PHP was running as an apache module, rather than PHP-CGI. I suppose as a work-around I could have used something like

    lynx -dump http://www.somedomain.com/cron.php
    

    For my use, I ended up installing php5-cli and then simply changing the cron job to

    php /path/to/file.php
    

    fixed it.