0 votes
in Website Design by (1.9k points)

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';
    } );

 

1 Answer

0 votes
by (1.9k points)

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 ;-)

...