I’m working on a newsletter signup form for my site and need to prevent users from registering with certain email providers. I want to block domains like @gmail, @yahoo, and @hotmail from being accepted when people try to subscribe.
Here’s my current signup processing code:
<?php
$admin_email = "[email protected]";
$email_subject = "New Subscription";
$redirect_url = "https://mysite.com/thanks";
$sender_email = $admin_email;
$user_email = $_POST['user_email'];
if (!strpos($user_email, '@') || !strpos($user_email, '.')) {
echo "Please enter a valid email address";
} else {
// Need validation logic here
}
$message_content = "New subscriber email: " . $_REQUEST['user_email'] . "\n";
mail($admin_email, $email_subject, $message_content, "From: $sender_email") or die("Failed to send email.");
header("Location: $redirect_url");
?>
What’s the best way to add domain filtering to reject these popular email providers? I need to check the domain part after the @ symbol and block the ones I don’t want to allow.
I’ve dealt with this before - blocking domains with an array works great. Just use explode() to grab the domain and check it against your blacklist:
$blocked_domains = array('gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com');
$email_parts = explode('@', $user_email);
if (count($email_parts) == 2) {
$domain = strtolower(trim($email_parts[1]));
if (in_array($domain, $blocked_domains)) {
echo "Registration with this email provider is not allowed";
exit;
}
}
Fair warning though - blocking major providers will tank your signup rates. We lost 60% of our conversions when we tried this. Really think about whether you need to block these or if there’s a better way to get what you want.
You could use regex for more flexible domain blocking. This should work:
$blocked_pattern = '/@(gmail|yahoo|hotmail|outlook)\.(com|net|org)$/i';
if (preg_match($blocked_pattern, $user_email)) {
echo "This email provider is not supported";
exit;
}
Regex catches variations like yahoo.net or outlook.org automatically without maintaining a huge array. But honestly, I’d rethink this whole strategy. You’re blocking most legitimate users since everyone has Gmail, Yahoo, etc. You’re basically turning away your actual audience.
Try double opt-in verification or a simple captcha instead. These filter out spam without alienating real subscribers who use popular email services.
honestly just use strpos if you want to block them. if(strpos($user_email, '@gmail.com') !== false || strpos($user_email, '@yahoo.com') !== false)
then reject it. simpler than regex or arrays but you’re shooting yourself in the foot marketing wise