# Reject Registrations From Email Domains That Cannot Receive Mail

> Checks that the email domain of every new registration has mail (MX) records, and refuses sign-ups from made-up or mistyped domains that bots and spammers often use.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: registration, spam, email
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/sec-registration-email-domain-check/

## Code

```php
$wpide_domain_has_mail = function ( $email ) {
    $domain = strtolower( (string) substr( strrchr( (string) $email, '@' ), 1 ) );

    if ( ! $domain || ! function_exists( 'checkdnsrr' ) ) {
        return true;
    }

    $key    = 'wpide_mx_' . md5( $domain );
    $cached = get_transient( $key );

    if ( false !== $cached ) {
        return 'yes' === $cached;
    }

    $ok = checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' );
    set_transient( $key, $ok ? 'yes' : 'no', DAY_IN_SECONDS );

    return $ok;
};

$wpide_mx_check = function ( $errors, $username, $email ) use ( $wpide_domain_has_mail ) {
    if ( is_wp_error( $errors ) && is_email( $email ) && ! $wpide_domain_has_mail( $email ) ) {
        $errors->add( 'wpide_email_domain', '<strong>Error:</strong> This email domain cannot receive email. Please check your address.' );
    }

    return $errors;
};

add_filter( 'registration_errors', $wpide_mx_check, 20, 3 );
add_filter( 'woocommerce_registration_errors', $wpide_mx_check, 20, 3 );
```

## Notes

Results are cached per domain for a day; if your server cannot do DNS lookups the check lets registrations through.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Reject Registrations From Email Domains That Cannot Receive Mail" 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/
