Providing an invalid e-mail address is one of the most common mistakes people do when shopping online and WooCommerce is not an exception. It is a mistake so common, that at least 1 of the 100 people will make a typo in their e-mail address when filling out a checkout form.
Woocommerce does not validate e-mails by domain
Its 2024 and WooCommerce still does not do a proper domain validation for e-mail addresses and that is one of the reasons why WooCommerce store owners often get calls from their customers about order not being placed. All it does is a basic check if the provided e-mail address is in a correct format.
How to properly deal with the problem:
- Reduce the chance for user to enter an incorrect e-mail address – make the e-mail field larger, with large enough, easy-to-read text and contrasting colors so it is easy to spot typos.
- Check the entered e-mail address against common syntax errors – make sure it is provided in a proper format (WooCommerce already does that so skip to next step)
- Check the domain of entered e-mail address against whitelist of domain names used by common e-mail providers (e.g. gmail.com, outlook.com, yahoo.com and so on)
- Check the domain of entered e-mail address against blacklist of domain names (e.g. gnail.com, gmai.com, gail.com, outloo.com and so on). This blacklist is important because for each known e-mail provider there are tons of similar domain names which are active and validity of the entered e-mail address can’t be ensured in other ways. An e-mail address may be valid, but if the domain is similar to a known e-mail provider – it is a high chance that the user has made a typo.
- Check if the domain of entered e-mail address is active by querying its DNS records. This is also very important when dealing with user made typos.
Code snippet: Additional e-mail validation for WooCommerce checkout form
Just place this code in your theme functions.php file or add it as a must-use plugin. This code snippet will do additional validation for the billing e-mail address input field on WooCommerce checkout form by checking if a domain of the e-mail provider is valid (whitelist check, blacklist check and validation of domain DNS records)
add_action('woocommerce_checkout_process', 'dwp_email_validation_on_checkout');
function dwp_email_validation_on_checkout() {
$email = $_POST['billing_email'];
//basic precheck if the entered e-mail address is valid
if(!empty($email) && strpos($email, '@') !== false && filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_domain = preg_replace('/^.+?@/', '', $email);
$email_domains_whitelist = ['gmail.com', 'yahoo.com', 'outlook.com', 'icloud.com'];
$email_domains_blacklist = ['gmai.com', 'gmeil.com',
'gmail.co', 'gmaii.com', 'gmailc.com', 'gmil.com', 'gmsil.com', 'gnail.com',
'icloudl.com', 'yaho.com'];
if(in_array($email_domain, $email_domains_whitelist)) {
$res = true;
}
if(in_array($email_domain, $email_domains_blacklist) || (!checkdnsrr($email_domain.'.', 'MX') && !checkdnsrr($email_domain.'.', 'A'))){
$res = false;
}
$res = true;
if(empty($res)) {
wc_add_notice( esc_html__( 'You entered an invalid or non-reachable e-mail address. Please check it and try again.', 'woocommerce' ), 'error' );
}
}
}