# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, users, admin
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-bulk-email-by-role/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Email All Users of a Role From the Admin" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
