How is your cron configured?
Server pings a certain .php file? Is it wp-cron.php? If not – make sure you completely load the WordPress (and there is no SHORT_INIT constant defined).
Hi Slava,
Thanks for getting back to me. It’s configured as below.
*/30 * * * * /usr/local/php70/bin/php-cli /home/public_html/wp-cron.php >/dev/null 2>&1
As you can see, there is nothing too fancy about it as it is just configured to run wp-cron.php every 30 mins using PHP7.
I have also checked to ensure SHORT_INIT is not defined anywhere.
So, 0.10.x works for you, and when you upgraded to 1.2.4 – it does not work, correct?
What about 0.11.2?
Can you provide your code you use to send email? I think something is with hooks and loading order. You may need to wrap your code to fire after init or later.
To make the WP Mail SMTP plugin work with default wp_mail() without redefining the function we redefine the global variable $phpmailer. So it might be done too late for your code. We are using plugins_loaded hook with priority 10, so make sure your email in code is sent using wp_mail() AFTER that hook fired.
@aaronroneill
Did you have a chance to review your code and check my recommendations?
Hi Slava,
Apologies for the delay in getting back to you. Here is the code to my sample script which the server cron runs. This script with any plugin version 0.11.1 or newer causes a root@localhost error.
As you can see it’s a very simple one file plugin that just schedules an email every 5 minutes, the server cron then sends the email.
<?php
/*
Plugin Name: Cron Tester
Plugin URI: #
Description: Simple cron task - check to see cron is running correctly
Version: 1.0
Author: Aaron
Author URI: #
*/
class aCronTester {
function __construct() {
# Actions:
add_action( 'a_test_cron', array( $this, 'send_mail' ) );
# Activation:
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
}
function send_mail() {
echo 'Cron Tester: send_mail() run! <br><br>';
error_log("Cron Tester Has Been Run: " . date('Ymd-H:i:s'), 0);
$mail = wp_mail( '[email protected]', 'a Cron Tester Run' . date('Ymd-H:i:s'), 'The Cron Tester has been run...' . date('Ymd-H:i:s'), '', array() );
error_log($mail);
echo 'Mail sent? ';
var_dump($mail);
}
function activate() {
wp_schedule_event( time(), 'every5min', 'a_test_cron' );
}
function deactivate() {
wp_clear_scheduled_hook( 'a_test_cron' );
}
}
add_filter('cron_schedules','cron_add_every_five');
function cron_add_every_five($schedules){
$schedules['every5min'] = array(
'interval' => 300,
'display' => __( 'Once every 5 minutes'),
);
return $schedules;
}
$acrontester = new aCronTester;
?>
Try to wrap your class initialization in a hook.
add_action( 'init', function () {
new aCronTester;
} );
During init we have already swapped phpmailer object with own logic.
Hi Slava,
I struggled with this issue for a bit. Tried the setup you advised with the class and instantiating is during init. The result was still the problem with the root@localhost email and Root User as sender.
When I called global $phpmailer inside the construct, it was actually an instance of the WPMailSMTP\MailCatcher class. So the plugin did get loaded before my class that needed to send the mail. What I did is set the From and FromName properties in the $phpmailer object. That did the trick.
To take the example of Aaron, it would look like this:
function __construct() {
global $phpmailer;
$phpmailer->From = ‘[email protected]’;
$phpmailer->FromName = ‘My from name’;
# Actions:
add_action( ‘a_test_cron’, array( $this, ‘send_mail’ ) );
# Activation:
register_activation_hook( __FILE__, array( $this, ‘activate’ ) );
register_deactivation_hook( __FILE__, array( $this, ‘deactivate’) );
}
-
This reply was modified 8 years, 3 months ago by
SdeWijs.
-
This reply was modified 8 years, 3 months ago by
SdeWijs.
Facing the same problem as well. It it is from root user. 🙁