Never Email Certain Addresses or Domains
Removes addresses and whole domains on your suppression list from every outgoing email, such as bouncing inboxes or people who asked not to be contacted, and skips emails left with no recipient.
Opens WPIDE on
Code
$wpide_suppressed = array( '[email protected]', '@old-company.com' );
$wpide_is_suppressed = function ( $recipient ) use ( $wpide_suppressed ) {
$email = strtolower( trim( preg_match( '/<([^>]+)>/', $recipient, $m ) ? $m[1] : $recipient ) );
foreach ( $wpide_suppressed as $entry ) {
$entry = strtolower( trim( $entry ) );
if ( $email === $entry || ( 0 === strpos( $entry, '@' ) && substr( $email, -strlen( $entry ) ) === $entry ) ) {
return true;
}
}
return false;
};
add_filter( 'wp_mail', function ( $atts ) use ( $wpide_is_suppressed ) {
$to = is_array( $atts['to'] ) ? $atts['to'] : explode( ',', (string) $atts['to'] );
$atts['to'] = array_values( array_filter( array_map( 'trim', $to ), function ( $recipient ) use ( $wpide_is_suppressed ) {
return '' !== $recipient && ! $wpide_is_suppressed( $recipient );
} ) );
return $atts;
}, 5 );
add_filter( 'pre_wp_mail', function ( $return, $atts ) {
return empty( $atts['to'] ) ? true : $return;
}, 5, 2 );
Notes
List full addresses or "@domain.com" entries; CC and BCC headers are not filtered.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.