WordPress sending emails continuously. How to stop it?

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

Solutions Collecting From Web of "WordPress sending emails continuously. How to stop it?"

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.