How do I test two email addresses for equality

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 "william.burroughs@gmail.com", "will.iam.burroughs@gmail.com", and "williamburroughs@gmail.com" 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 "william.burroughs@gmail.com" and "williamburroughs@gmail.com" to be the same. I can start special casing something like "gmail" so they are but I was hoping there is a better approach.

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.


If you're comparing 2 different e-mails addresses just as characters, you may want to consider using regex to split them up for comparisons. A simple enough one could work as "([\w\.]+)@([\w\.]+\.\w+)". You could run group 2 through a switch to compare group 1 appropriately, defaulting to a more general comparison.

boolean emailsEquals(String email1,String email2) {
    Pattern address=Pattern.compile("([\\w\\.]+)@([\\w\\.]+\\.\\w+)");
    Matcher match1=address.matcher(email1);
    Matcher match2=address.matcher(email2);
    if(!match1.find() || !match2.find()) return false; //Not an e-mail address? Already false
    if(!match1.group(2).equalsIgnoreCase(match2.group(2))) return false; //Not same serve? Already false
    switch(match1.group(2).toLowerCase()) {
    case "gmail.com":
        String gmail1=match1.group(1).replace(".", "");
        String gmail2=match2.group(1).replace(".", "");
        return gmail1.equalsIgnoreCase(gmail2);
    default: return match1.group(1).equalsIgnoreCase(match2.group(1));
    }
}

Hope it helps!


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...


Here this works but maybe I didnt understand your question

    String email1 = new String("william.burroughs@gmail.com");
    String email2 = new String("williamburroughs@gmail.com");

     Pattern emailFinder = Pattern.compile("gmail.com");
     Matcher emailmatcher = emailFinder.matcher(email1);
     Matcher emailmatcher1 = emailFinder.matcher(email2);
     if (emailmatcher.find() && emailmatcher1.find()) {

            email1 = email1.replaceAll(".","");
            email2 = email2.replaceAll(".","");

            if(email1.equals(email2)){
                System.out.println("The values match");
            }

     }

Need Your Help

Using sections in CodeIgniter

php asp.net-mvc-3 codeigniter partial-views template-engine

I'm using ASP.NET MVC3 at work, while using PHP at home. I wish to use the Sections feature of MVC3 in CodeIgniter, does it have such feature?