title: Need help checking if to emails are part of the same domain?
body: <p>Background information:</p>
  
  <p>I need to make a system that lets users log in by identifying themselves with an email address/password combination. So I'm trying to find a way to check that the login matches something in the database, when the two may be the same address that's typed differently.</p>
  
  <p>So the initial steps for the domain verification are:</p>
  
  <ul>
  <li>Convert the entered text to NFC</li>
  <li>Strip comments with a recursive descent parser</li>
  <li>split the local part from the domain.</li>
  <li><p>run the domain through this:</p>
  
  <pre><code>nslookup -type=mx $DOMAIN | awk '{ if($2 =="mail" &amp;&amp; $3 == "exchanger") { print substr($6, 0, length($6)-1) } else if($2 == "internet" || $2 == "has") { print $1 } }' | sort | uniq'
  </code></pre>
  
  <p>Which for both <code>me.com</code> and <code>iCloud.com</code> returns:</p>
  
  <pre><code>mx1.mail.icloud.com
  mx2.mail.icloud.com
  mx3.mail.icloud.com
  mx4.mail.icloud.com
  mx5.mail.icloud.com
  mx6.mail.icloud.com
  </code></pre>
  
  <p>And <code>gmail.com</code> and <code>googlemail.com</code> both return:</p>
  
  <pre><code>alt1.gmail-smtp-in.l.google.com
  alt2.gmail-smtp-in.l.google.com
  alt3.gmail-smtp-in.l.google.com
  alt4.gmail-smtp-in.l.google.com
  gmail-smtp-in.l.google.com
  </code></pre></li>
  </ul>
  
  <p>So I thought I would enter this into a table so that all the gmail servers have id 1, and all the iCloud servers have id 2, etc. in whatever order people try to register them in.</p>
  
  <p>This doesn't work if all the servers are in one varchar unit, because servers could be added or removed at a later date.  And it doesn't really work if the servers are all given their own row because there's no way to manipulate them as if they were a single logical unit.</p>
  
  <p>If I get the longest common postfix of each server name would that indicate that these all use one mail server? or would it be possible for that to actually serve two servers separated by subdomains?</p>