Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email validation MX Lookup

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!

like image 220
Paul Avatar asked Nov 03 '09 11:11

Paul


2 Answers

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.

like image 97
bobince Avatar answered Oct 21 '22 09:10

bobince


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

like image 23
jitter Avatar answered Oct 21 '22 07:10

jitter