Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test two email addresses for equality

Tags:

java

email

Is there a comprehensive way to test if two email addresses are equal? I know that I can universally LOWER both. But there are some other rules that differ from server to server. For example "[email protected]", "[email protected]", and "[email protected]" are all equivalent for gmail. But I don't think this is true in all cases. So given two email addresses I need to ensure they are equivalent. Currently my code does not consider "[email protected]" and "[email protected]" to be the same. I can start special casing something like "gmail" so they are but I was hoping there is a better approach.

like image 414
robert_difalco Avatar asked Nov 11 '22 16:11

robert_difalco


2 Answers

Gmail only really has two rules for customization

  1. Any periods ( . ) are ignored. This is easily overcome with a regex for gmail addresses.
  2. Anything after a + is ignored. Again. a regex will fix this for gmail addresses.

The biggest challenge as I see it is that Google hosts thousands of cloud based domains that do not end in googlemail.com or gmail.com. Your only way to recognize these would be to do a DNS lookup and see what MX record the domain points to. Here's a python example that works:

http://eran.sandler.co.il/2011/07/17/determine-if-an-email-address-is-gmail-or-hosted-gmail-google-apps-for-your-domain/

You could do the same in any other language. Look at the 'MX' record for gmail or googlemail.

For any non-google domains, you can do a lowercase string compare.

like image 197
David S. Avatar answered Nov 14 '22 22:11

David S.


A part from a custom coded set of rules for given email providers (e.g gmail for your example) I don't think there is any other way...

like image 23
Harry Avatar answered Nov 14 '22 23:11

Harry