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.
Opens WPIDE on
Code
$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 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.