# Clean Up Email Recipient Lists Before Sending

> Removes duplicate, malformed and placeholder addresses (such as example.com or .test domains) from email recipient lists, so one bad address does not make the whole email fail.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, validation, deliverability
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-clean-recipient-list/

## Code

```php
$wpide_reserved_domains = array( 'example.com', 'example.org', 'example.net', 'test', 'invalid', 'localhost', 'local' );

add_filter( 'wp_mail', function ( $atts ) use ( $wpide_reserved_domains ) {
    $list  = is_array( $atts['to'] ) ? $atts['to'] : explode( ',', (string) $atts['to'] );
    $clean = array();

    foreach ( $list as $recipient ) {
        $recipient = trim( (string) $recipient );
        $email     = strtolower( preg_match( '/<([^>]+)>/', $recipient, $m ) ? trim( $m[1] ) : $recipient );
        $domain    = (string) substr( strrchr( $email, '@' ), 1 );
        $tld       = substr( strrchr( '.' . $domain, '.' ), 1 );

        if ( ! is_email( $email ) || isset( $clean[ $email ] ) || in_array( $domain, $wpide_reserved_domains, true ) || in_array( $tld, $wpide_reserved_domains, true ) ) {
            continue;
        }

        $clean[ $email ] = $recipient;
    }

    $atts['to'] = array_values( $clean );

    return $atts;
}, 6 );
```

## Notes

Only the To list is cleaned; edit the reserved domains at the top if you really send to any of them.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Clean Up Email Recipient Lists Before Sending" 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/
