I have a cron cron job setup to send emails. It is executed using the following command:
/usr/bin/php -q /var/www/vhosts/domain.com/cron/job.php
When I execute the php script from a browser, the script sends emails as expected. It fails when run by the cron job.
I don’t see any errors in the maillog. Only difference is in the “from” line. When the script is executed from a browser, the log reads:
Jan 25 12:24:39 VMW01 qmail-remote-handlers[1001]: from=sender@domain.com
But when the cron job executes the script the “from” line becomes:
Jan 25 12:15:01 VMW01 qmail-queue-handlers[1000]: from=anonymous@VMW01.server.net
Oddly, the cron job will send emails when I remove the fifth parameter of the mail() line.
mail($email, $subject, $body, $headers, '-fsender@domain.com');
I would simply live with this, but if I remove the parameter, emails are often rejected by servers – which I presume is because the parameter is not set.
Any idea why this is happening and how I can fix it? I’d be happy to add more detail to this question in order to get to a solution.
As @Michael said, most likely your cron job is using a different php.ini file. You can use the
php_ini_loaded_file()function to see what file is loaded in your script – then run it from the command line, or your cron to see the difference with the web execution.My guess is that the problem arises because the php.ini for the cron job (same as command line) has safe_mode set, and when that is the case, you shouldn’t use the last parameter to the
mail()function as it can fail. As evidence, if you look at thePHPMailerclass there is amail_passthru()method that simply calls the PHPmail()function under the covers. Notice that it leaves out the extra parameter when not insafe_mode:You can load a specific php.ini file when calling php from the command line (or your cron job) like this: