Forum Moderators: coopster

Message Too Old, No Replies

validating email adresses - the new way

         

Adrian2k4

1:12 am on Oct 4, 2004 (gmt 0)

10+ Year Member



the email verification was on my to-do list when i was surfing around on the php manual when i found the function "checkdnsrr()".
i thought: cool - thats my email validator.

simply take the part after the "@" of the email address i.e. the domain of the email address and do the following:

if (checkdnsrr($domain, "MX")) {
echo "valid email"
} else {
echo "invalid email"
}

i still need to combine the MX-check with some sort of regex check to see if the format of the email address is valid, but its impossible to submit an address with an invalid host (user can still be wrong)

mincklerstraat

9:05 am on Oct 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the tip, Adrian2k4. Haven't seen this one before. You might also want to make it check for 'example.com' to boot, since this is the official 'test' domain. I always use 'example.com' when a form asks me for an e-mail address but there's no good reason to (i.e., they might be address collecting for spam). Downside of checking for example.com is users who input an e-mail address with this as the domain are probably likely to input a nonsense address when they're refused, and then you have another nonsense address in your records. Probably best practice to allow example.com, and weed this out later.

elklabone

2:56 pm on Oct 4, 2004 (gmt 0)

10+ Year Member



another good domain to use if you're afraid you'll get spam is @mailinator.com

You can check that mail if you want, but it's a temporary "throw away" account. Very cool.

--Mark

ergophobe

9:35 pm on Oct 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I wouldn't bother checking for example.com or any such thing. The number of people who know that example.com is reserved by ICAAN for testing is probably way smaller than the number of people like me who simply type in addresses like "bob@hotmail.com" or bill@microsoft.com. Poor Bob (Bill is not poor).

In the long run, you need to send an email to the address and have the user take action to verify that he/she wanted to join your group.

The point of checkdnsrr() is that it's worthwhile as an aid to the well-intentioned subscriber who accidentally types bill@mcirosfot.com. You can then say - "Sorry try another address".

Tom

Romeo

10:17 pm on Oct 4, 2004 (gmt 0)

10+ Year Member



Hi Adrian,

thank you for this one -- didn't knew and didn't use this dns-function so far.

However, this seems not to be a genuine "email address" check, as not every domain expecting incoming mail may have an MX record defined, and AFAIK in case of an absent MX record, a sending MTA would simply try to deliver to the A address of a domain instead.

So this means there may be valid mail addresses in domains without MX records, and your check seems just to verify, if a domain zone maintainer has properly defined an MX record or missed that in some sort of sloppyness.

Regards,
R.

Adrian2k4

11:36 pm on Oct 4, 2004 (gmt 0)

10+ Year Member



@Romeo
since the point is to check if the domain existes, you could also check the A record and the MX insead of only the MX record to be sure.

i.e.:

if (checkdnsrr($domain, "MX") OR checkdnsrr($domain, "A")) {
echo "valid email";
} else {
echo "invalid email";
}

Note there are domains that have a MX but no A record. (i.e. inactive Homepages that still catch email)
Also note that the whole thing only works on a linux server... but who cares, everybody has linux.

Romeo

8:35 am on Oct 5, 2004 (gmt 0)

10+ Year Member



Hi Adrian,

yes, looking for both "MX" and "A" should do the trick.
My previous statement was just to ensure not to rely only on "MX" checking.
And for the Linux: yes, it would be hard not to have it.

Regards,
R.

Adrian2k4

5:12 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



since the type is optional using
if (checkdnsrr($domain)) {
//snip
} else {
//snip
}

would be best. this also fixes problems that would arise if the domain uses a cname entry etc.

Timotheos

5:34 pm on Oct 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the example provided in O'Reilly database book. I've used it for quite sometime with no problems.

if (!( @getmxrr($maildomain, $temp) ¦¦ @gethostbyname($maildomain)!= $maildomain ))
echo "The domain does not exist.";

Netizen

6:12 pm on Oct 5, 2004 (gmt 0)

10+ Year Member



The one point to be wary of is the time it can take to look up MX records - not all DNS servers are fast or even guaranteed to be the 100% of the time.