Title: Plugin does not work when sending mail from wp-cli script
Last modified: February 7, 2022

---

# Plugin does not work when sending mail from wp-cli script

 *  Resolved [Christiaan van Luik](https://wordpress.org/support/users/cvl01/)
 * (@cvl01)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/plugin-does-not-work-when-sending-mail-from-wp-cli-script/)
 * Hello!
 * I noticed that this plugin does not working when sending a email from a WP-CLI
   script. This is the case for any custom wp-cli command that has the wp_mail function
   in it.
 * I get the following error:
    `Error: Call to a member function wp_mail() on null`
   on `line 53` in `smtp-mailing-queue/smtp-mailing-queue.php`.
 * I think this has something to do with global variables working slightly different
   in WP CLI.
 * I was able to fix it with these modifications:
 * in `smtp-mailing-queue.php`:
 *     ```
       if (!function_exists('wp_mail') && !isset($_GET['smqProcessQueue'])) {
           function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
           {
               $originalPluggeable = OriginalPluggeable::get_instance();
               $smtpMailingQueue = SMTPMailingQueue::get_instance(__FILE__, $originalPluggeable);
               return $smtpMailingQueue->wp_mail($to, $subject, $message, $headers, $attachments);
           }
       }
       ```
   
 * In the `OriginalPluggeable` and `SMTPMailingQueue` classes I added this field
   and `get_instance` method:
 *     ```
       private static $instance = null;
       ```
   
 *     ```
       public static function get_instance($pluginFile = null, OriginalPluggeable $originalPluggeable) {
           if ( null == self::$instance ) {
               self::$instance = new self($pluginFile, $originalPluggeable);
           }
           return self::$instance;
       }
       ```
   
 *     ```
       public static function get_instance() {
           if ( null == self::$instance ) {
               self::$instance = new self();
           }
           return self::$instance;
       }
       ```
   
 * I hope you can fix this problem using the above solution or another solution.
    -  This topic was modified 4 years, 3 months ago by [Christiaan van Luik](https://wordpress.org/support/users/cvl01/).
      Reason: Remove nonfunctional markdown

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Birmania](https://wordpress.org/support/users/birmania/)
 * (@birmania)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/plugin-does-not-work-when-sending-mail-from-wp-cli-script/#post-15339207)
 * Hi [@cvl01](https://wordpress.org/support/users/cvl01/),
 * Thanks for this nice and clear bug report with code suggestion.
    Your code seems
   very logic but I found that we just have to explicitely globalize ‘$smtpMailingQueue’
   in order for WP-CLI to find it.
 * See : [https://github.com/wp-cli/wp-cli/issues/4019](https://github.com/wp-cli/wp-cli/issues/4019)
 * I just published v1.4.6 with the associated fix.
    Waiting for your feedback, 
   ensuring that your problem is solved.
 * Yours faithfully,
    Birmania
 *  Thread Starter [Christiaan van Luik](https://wordpress.org/support/users/cvl01/)
 * (@cvl01)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/plugin-does-not-work-when-sending-mail-from-wp-cli-script/#post-15340315)
 * That is a nice and easy fix you did, I can confirm it works properly.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Plugin does not work when sending mail from wp-cli script’ is closed 
to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/smtp-mailing-queue.svg)
 * [SMTP Mailing Queue](https://wordpress.org/plugins/smtp-mailing-queue/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/smtp-mailing-queue/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/smtp-mailing-queue/)
 * [Active Topics](https://wordpress.org/support/plugin/smtp-mailing-queue/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/smtp-mailing-queue/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/smtp-mailing-queue/reviews/)

## Tags

 * [bugfix](https://wordpress.org/support/topic-tag/bugfix/)
 * [find](https://wordpress.org/support/topic-tag/find/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [member](https://wordpress.org/support/topic-tag/member/)
 * [not-found](https://wordpress.org/support/topic-tag/not-found/)
 * [NULL](https://wordpress.org/support/topic-tag/null/)
 * [wp-cli](https://wordpress.org/support/topic-tag/wp-cli/)

 * 2 replies
 * 2 participants
 * Last reply from: [Christiaan van Luik](https://wordpress.org/support/users/cvl01/)
 * Last activity: [4 years, 3 months ago](https://wordpress.org/support/topic/plugin-does-not-work-when-sending-mail-from-wp-cli-script/#post-15340315)
 * Status: resolved