Halloween Sale 15% off Ends in See the plans
PHP

Email All Users of a Role From the Admin

Adds Users > Email Users, where administrators can write a plain-text message and send it to every user with a chosen role, delivered in batches in the background.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_menu', function () {
    add_users_page( 'Email Users', 'Email Users', 'manage_options', 'wpide-email-users', function () {
        $sent = false;

        if ( isset( $_POST['wpide_bulk_subject'] ) && check_admin_referer( 'wpide_bulk_email' ) && current_user_can( 'manage_options' ) ) {
            $role    = sanitize_key( wp_unslash( isset( $_POST['wpide_bulk_role'] ) ? $_POST['wpide_bulk_role'] : '' ) );
            $subject = sanitize_text_field( wp_unslash( $_POST['wpide_bulk_subject'] ) );
            $message = sanitize_textarea_field( wp_unslash( isset( $_POST['wpide_bulk_message'] ) ? $_POST['wpide_bulk_message'] : '' ) );

            if ( get_role( $role ) && $subject && $message ) {
                $job = 'wpide_bulk_' . wp_generate_password( 8, false );
                set_transient( $job, array( 'role' => $role, 'subject' => $subject, 'message' => $message ), DAY_IN_SECONDS );
                wp_schedule_single_event( time(), 'wpide_bulk_email_batch', array( $job, 0 ) );
                $sent = true;
            }
        }

        echo '<div class="wrap"><h1>Email Users</h1>';
        if ( $sent ) {
            echo '<div class="notice notice-success"><p>Your message is being sent in the background.</p></div>';
        }
        echo '<form method="post">';
        wp_nonce_field( 'wpide_bulk_email' );
        echo '<table class="form-table"><tr><th><label for="wpide_bulk_role">Send to</label></th><td><select name="wpide_bulk_role" id="wpide_bulk_role">';
        wp_dropdown_roles( 'subscriber' );
        echo '</select></td></tr><tr><th><label for="wpide_bulk_subject">Subject</label></th><td><input name="wpide_bulk_subject" id="wpide_bulk_subject" class="regular-text" required></td></tr>';
        echo '<tr><th><label for="wpide_bulk_message">Message</label></th><td><textarea name="wpide_bulk_message" id="wpide_bulk_message" rows="10" class="large-text" required></textarea></td></tr></table>';
        submit_button( 'Send' );
        echo '</form></div>';
    } );
} );

add_action( 'wpide_bulk_email_batch', function ( $job = '', $offset = 0 ) {
    $data = get_transient( (string) $job );
    if ( ! is_array( $data ) ) {
        return;
    }

    $emails = get_users( array( 'role' => $data['role'], 'fields' => 'user_email', 'number' => 40, 'offset' => (int) $offset, 'orderby' => 'ID' ) );
    if ( ! $emails ) {
        delete_transient( (string) $job );
        return;
    }

    wp_mail( get_option( 'admin_email' ), $data['subject'], $data['message'], array( 'Bcc: ' . implode( ',', array_filter( $emails, 'is_email' ) ) ) );
    wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wpide_bulk_email_batch', array( (string) $job, (int) $offset + 40 ) );
}, 10, 2 );

Notes

Messages go out as BCC batches of 40 every minute through WP-Cron; recipients' addresses are never shown to each other.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.