Reputation: 326
I have a wordpress site which has installed EasySMTP plugin.
I have a real cron task and I have unactive wp simulated cron.
My cron is running, but inside it I want to make a mail send and is not working.
In my log, I see this:
[20-Apr-2017 14:46:02 UTC] PHP Fatal error: Uncaught exception 'phpmailerException' with message 'Invalid address: (setFrom) wordpress@[domain]' in /home/user/public_html/wp-includes/class-phpmailer.php:1023
Stack trace:
#0 /home/user/public_html/wp-includes/pluggable.php(352): PHPMailer->setFrom('wordpress@[domain...', 'WordPress', false)
#1 /home/user/public_html/wp-content/plugins/innovation-factory/includes/php/functions.php(350): wp_mail('msolla@domain...', 'Tienes 1 ideas ...', '\n\t\t<html>\n\t\t<bo...', Array)
#2 /home/user/public_html/wp-content/plugins/innovation-factory/innovation-factory.php(301): enviarNotificacion('msolla@domain...', 'Tienes 1 ideas ...', NULL, Array, '\n\t\t<html>\n\t\t<bo...')
#3 [internal function]: do_this_hourly()
#4 /home/user/public_html/wp-includes/class-wp-hook.php(298): call_user_func_array('do_this_hourly', Array)
#5 /home/user/public_html/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array)
#6 /home/des in /home/user/public_html/wp-includes/class-phpmailer.php on line 1023
Obviusly I have other smtp configuration in EasySMTP. In fact, I have an smtp hosted in other server with other domain diferent to de web page domain.
If I call do_this_hourly() function out of the cron, it sends correctly the emails. I need do something different to mail in cron tasks? Maybe that wp-cron.php not load EasySMTP yet?
My cron is programmed with this command:
php /home/user/public_html/wp-cron.php
Thanks!
Upvotes: 1
Views: 459
Reputation: 926
This seems to come from the fact that $_SERVER['SERVER_NAME'] is not set when mail is sent using cron via crontab, cli or some other command line script.
$_SERVER['SERVER_NAME']
throws a notice, $sitename
is an empty string, and $from_email
is simply 'wordpress@'.
WordPress does at least give you a chance to set the $from_email
before it attempts to send, in which case I've added a filter which seems to work for me.
add_filter( 'wp_mail_from', function( $from_email ) {
// Domain ends with an @. Append the domain from the site url.
if ( substr( $from_email, -1 ) === '@' ) {
// Trim protocol, double forward slash, preceeding www, port, and remaind of request uri.
$from_email .= preg_replace( '#^(?:[hftps]*:)?//(?:www\.)?|[:/?].*$#i', '', get_site_url() );
}
return $from_email;
} );
Upvotes: 2
Reputation: 1614
$_SER the problem is, that php doesn't get the $_SERVER[ 'SERVER_NAME' ] when executed via crontab. And that's why the phpmailer throws that exception.
I solved it by passing the variable manually in crontab. F.e:
*/15 * * * * export SERVER_NAME="server.domain.name" && php /var/www/clients/client2/web8/web/wp-base/wp-cron.php
A detailed description of this problem can be found here https://www.ask-sheldon.com/wordpress-cron-using-wp_mail-function/.
Upvotes: 1