views:

388

answers:

2

I have been asked to implement some email address validation on a web app - I'm sure we've all been there a thousand times... however, this time I have been asked to do an MX Lookup on the domain to see if it accepts emails.

Does anyone know of any potential problems with doing this? Is an mx lookup a reliable way of finding out if a domain accepts email? are there any edge cases where a valid email address could fail an MX lookup check?

Thanks for your advice!

+1  A: 

You can only check if there is an mail server registered for the domain.

If the server also accepts mails and if the address is valid (not syntactically but in the sense that there exists a inbox for it and so on...) you will only find out when sending the e.g. registration email

sample on how to do this in PHP

function mailserver_exists($email) {
 list($user,$domain) = split('@',$email);
 //included check for 'A' after [comment from bobince][1]
 return checkdnsrr($domain,'MX') || checkdnsrr($domain,'A');
}
if(domain_exists('[email protected]')) {...} else {...}

Yes you can use '[email protected]' too. The PHP documentation for checkdnsrr(host, type) states

host may either be the IP address in dotted-quad notation or the host name

jitter
Hi Jitter - thanks for the response.Do you know if MX lookups will work if the email address is using IP Address: something like [email protected]
Paul
expanded answer to answer your additional question
jitter
+1  A: 

are there any edge cases where a valid email address could fail an MX lookup check?

Yes, in that where there is no MX record, MTAs fall back to using the A record instead. So only allowing MX records would fail a class of mail servers that work in the real world.

Allowing any name with an MX or A record at least detects obvious mistypings that result in NXDOMAIN. However it will still allow mistypings that end up at squatter sites. A further step for addresses resolved by A records might be to check where port 25 is accepting connections on that address.

bobince