Support » Plugin: WP Mail SMTP by WPForms » Mails Incorrectly sent as Root User (root@localhost)

  • Hey,

    I have run into an issue with your plugin since upgrading from version 0.10.1. Since upgrading from this version, all emails which are sent out via the server cron (not WordPress cron) are show the mail from and name as Root User (root@localhost).

    I have set these in the plugins configuration and have made sure these are saved. I even checked the database to confirm this.

    If I downgrade the plugin back to 0.10.1, the emails are sent out with the correct from address and name. As soon as I upgrade to the next version up (0.11.1 >) this no longer works.

    If I manually send an email through a function call without it running through a cron then this works no problem. It also works if the WP cron runs the function which sends out an email. It only doesn’t work when the server is the active cron.

    This is using the Other SMTP method, with defined values. We have been using this plugin for years without any issues until we upgraded from 0.10.1.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Slava Abakumov

    (@slaffik)

    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.

    Plugin Author Slava Abakumov

    (@slaffik)

    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.

    Plugin Author Slava Abakumov

    (@slaffik)

    @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( 'a@fakeemail.com', '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;
    ?>
    Plugin Author Slava Abakumov

    (@slaffik)

    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 = ‘info@example.nl’;
    $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 1 year, 9 months ago by SdeWijs.
    • This reply was modified 1 year, 9 months ago by SdeWijs.

    Facing the same problem as well. It it is from root user. 🙁

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Mails Incorrectly sent as Root User (root@localhost)’ is closed to new replies.