1

Recently I'm going to test wp_mail function. I wrote below code into a plugin file: wp_mail( 'me@example.net', 'The subject', 'The message' );

and I entered my email instead of me@example.net. Now WordPress continuously send email to me. I don't know how to stop it! There are over 1000 mails in just 15 minutes. Please help me. I disabled all plugins and even delete pluggable.php file.

The context is :

function twp_mail_action($result, $to, $cc, $bcc, $subject, $body){
    $nt = new Notifygram();
    $_apikey = get_option('twp_api_key');
    $_apitoken = get_option('twp_api_token');
    $_projectname = get_option('twp_project_name');
    $nt->Notifygram($_apikey,$_apitoken, $_projectname );
    $nt->notify($body);
    wp_mail( 'myemail@gmail.com', 'The subject', 'The message' );
}
add_action( 'phpmailer_init', function( $phpmailer ){
    $phpmailer->action_function = 'twp_mail_action';
    } );
2
  • Where is the code? Please post it in context.
    – s_ha_dum
    Jul 3, 2015 at 18:59
  • @s_ha_dum I added it to question. Please help me! Jul 3, 2015 at 19:05

1 Answer 1

8

The problem

I think the problem here is that you're generating an infinite loop, by placing wp_mail() inside the $phpmailer->action_function callback, that fires after each email.

Each time you send an email with wp_mail(), you call wp_mail() again, and again, and again ... etc

Possible workaround

You should instead use, for example, the following approach:

function twp_mail_action($result, $to, $cc, $bcc, $subject, $body){

    // Here we remove the phpmailer_init action callback, to avoid infinite loop:
    remove_action( 'phpmailer_init', 'wpse_phpmailer_init' );

    // Send the test mail:
    wp_mail( 'myemail@gmail.com', 'The subject', 'The message' );

    // Here we add it again
    add_action( 'phpmailer_init', 'wpse_phpmailer_init' );

}

add_action( 'phpmailer_init', 'wpse_phpmailer_init' );

function wpse_phpmailer_init( $phpmailer )
{
    $phpmailer->action_function = 'twp_mail_action';
} 

Note that this is untested, so be careful testing this only on a server where you can clear the mail queue yourself, if mistakes are made in the testing ;-)

Better:

Log your tests to a file instead, for example.

Limit the delivery of emails

It would be interesting if we could limit the number of sent emails, per page load, while doing tests.

Here's one idea for such a plugin:

<?php
/**
 * Plugin Name: Limit Delivered Emails
 * Description: Set an upper limit to number of sent emails per page load.
 * Plugin URI:  https://wordpress.stackexchange.com/a/193455/26350
 */
add_action( 'phpmailer_init', function( $phpmailer )
{
    $max_emails_per_page_load = 10; // <-- Edit this to your needs!

   if( did_action( 'phpmailer_init' ) > $max_emails_per_page_load )
       $phpmailer->ClearAllRecipients();

} );

where we clear the to/cc/bc fields, with the ClearAllRecipients() method, to stop the email delivery.

We could also throw an uncaught error with:

throw new \phpmailerException( __( 'Too many emails sent!' ) );

instead of using:

$phpmailer->ClearAllRecipients();

This is related to my answer here regarding the use of the PHPMailer class.

14
  • Now how can I stop it?! Deleting all wordpress files? Jul 3, 2015 at 19:14
  • Just don't call wp_mail() inside $phpmailer->action_function as explained in the answer.
    – cybmeta
    Jul 3, 2015 at 19:15
  • @cybmeta I've deleted the whole plugin files and pluggable.php and class-phpmailer.php but there is no success Jul 3, 2015 at 19:18
  • 3
    I suspect the php process is long dead but that while it was alive it filled up your e-mail server with a very long queue. You could try to kill the e-mail server. If you don't know how do that, I'd recommended contacting your host. Though I suspect (hope) the e-mails would dry up before you would get a response... Jul 3, 2015 at 19:21
  • 1
    @AmirMousavi great to hear that, so hopefully you can tell your grandchildren a funny story about this adventure, in the future ;-)
    – birgire
    Jul 4, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.