This is a tricky thing, and it's difficult or impossible to do correctly with a regular expression, as it gets out of hand quickly. You will have to weigh concerns about false-positives and -negatives when designing your filter, and make any decision based on what you prefer. It is incorrect to think that this kind of filter will work 100% of the time.
Based on your requirements, you should make a decision to:
- Filter aggressively, and be fine with some people not getting emails from you, or
- Don't filter at all, but remove addresses that bounce from the mailing list.
It, again, depends on your requirement, but I recommend not filtering. Even in cases where email reputation is a concern, unless you're sending emails to equal numbers of good and bad addresses, this is the better option.
A few points to demonstrate this fact
Unlike what you posted:
admin@gmail.com
is an illegal address
postmaster.@gmail.com
will receive mail.
This demonstrates that it is very hard to get things like this right. And that (in my opinion) you shouldn't try. Even "simple" and "obvious" things are often anything but in the Wacky World of Email®.
It's important to note that dots don't matter in gmail
addresses.
Gmail doesn't recognize dots as characters within usernames, you can
add or remove the dots from a Gmail address without changing the
actual destination address; they'll all go to your inbox, and only
yours. In short:
homerjsimpson@gmail.com = hom.er.j.sim.ps.on@gmail.com
homerjsimpson@gmail.com = HOMERJSIMPSON@gmail.com
homerjsimpson@gmail.com = Homer.J.Simpson@gmail.com
A quick test on my personal email has confirmed that emails with
leading or trailing dots respect this principle:
homerjsimpson@gmail.com = .homerjsimpson@gmail.com
homerjsimpson@gmail.com = homerjsimpson.@gmail.com
homerjsimpson@gmail.com = homerjsimpson.....@gmail.com
work, and are delivered.
You must distinguish between valid Gmail username, and valid Gmail address. They are not the same thing.
Just because you cannot register with certain string for a username does not mean that putting that same string in front of @gmail.com
won't deliver an email.
Some other points:
- Usernames must be at least 6 characters. This means
admin@gmail.com
is, in fact, an illegal address. bob@gmail.com
, etc. are also illegal according to this guideline, although "obviously legal".
- Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.) You should
allow any combination of these in the username if you decide on a regex filter. And also the plus ('+'), and probably some other characters we haven't considered.
- There are also max-length of username, total length of address constraints, and other constraints on emails in general.
- Plus signs are not legal parts of Gmail usernames, but can be included in gmail addresses.
homerjsimpson+stackoverflow@gmail.com
will happily be delivered to homerjsimpson@gmail.com
.