wordpress cron: using wp_mail function

Today I had to solve the problem, that my wordpress cron throws an exception like this when sending an email via wp_mail function inside a cron scheduled event function.

PHP Fatal error:  Uncaught exception 'phpmailerException' with message 'Invalid address:  (setFrom) wordpress@' in /var/www/clients/client2/web8/web/wp-base/wp-includes/class-phpmailer.php:1023
Stack trace:
#0 /var/www/clients/client2/web8/web/wp-base/wp-includes/pluggable.php(352): PHPMailer->setFrom('wordpress@', 'WordPress', false)
#1 /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/sheldon_misc/admin/class-sheldon_misc-admin.php(183): wp_mail('info@ask-sheldon.com....', 'Statusmailing i...', 'The cron for im...')
#2 /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/sheldon_misc/admin/class-sheldon_misc-admin.php(143): Sheldon_Misc_Admin::sendStatusMail('ERROR', 0, 'Something went ...')
#3 [internal function]: Sheldon_Misc_Admin->process_hourly_cron()
#4 /var/www/clients/client2/web8/web/wp-base/wp-includes/class-wp-hook.php(298): call_user_func_array(Array, Array)
#5 /var/www/clients/client2/web8/web/wp-base/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array)
#6 /var/www/clients/cli in /var/www/clients/client2/web8/web/wp-base/wp-includes/class-phpmailer.php on line 1023

The wordpress cron was registered like that:

wp_schedule_event( time(), 'hourly', 'intolife_misc_hourly_cron_event' );

And the wordpress cron mechanism was triggered via Linux crontab:

*/15 * * * * php /var/www/clients/client2/web8/web/wp-base/wp-cron.php

Problem with wp_mail in wordpress cron

The problem was, that the PHP server variable $_SERVER[ ‘SERVER_NAME’ ]  isn’t available when executed via crontab, unix shell as well as wp-cli wrapper. That’s the reason why the phpmailer  didn’t get the hostname for the from parameter, which results in trying to send  mail with “wordpress@” as from value. And that’s the trigger for the exception mentioned above.

Solution for wp_mail in wordpress cron

The solution was relatively simple, after determining what the real reason was. I just had to pass the server variable to PHP inside the crontab call.

*/15 * * * * export SERVER_NAME="ask-sheldon.com" && php /var/www/clients/client2/web8/web/wp-base/wp-cron.php

The first part before the && sets the required environment variable. The second part stayed as it was and just calls the cron a usual.

Similar Problem

I had the same issue with wc-super-cache:

PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576
PHP Warning:  strpos(): Empty needle in /var/www/clients/client2/web8/web/wp-base/wp-content/plugins/wp-super-cache/wp-cache-phase1.php on line 576

It was only a warning but hence I got all output via mail, it was really annoying. 😉
Once more I had to define an environment variable on my own:

*/15 * * * * export REQUEST_URI='/' && export SERVER_NAME="ask-sheldon.com" && php /var/www/clients/client2/web8/web/wp-base/wp-cron.php

As you can see, the missing environment variable was $_SERVER[ ‘REQUEST_URI’ ]  in that case.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.