Email Address Verification with PHP - Using getmxrr() for validation (
Page 5 of 6 )
It’s possible to use the getmxrr() PHP function to achieve email domain validation similar to that obtained with checkdnsrr(). This function gets MX records corresponding to a given Internet host name and has the following format:
int getmxrr ( string hostname, array mxhosts [, array weight]);
It searches the DNS for MX records corresponding to the given host name. It returns true if any records are found and returns false if no records are found or if an error occurs.
A list of the MX records found is placed into the array mxhosts. If the weight array is given, it will be filled with the weight information gathered.
Having presented this network function, it’s easily deducible that we could rewrite our checkEmail() function utilizing getmxrr() instead of checkdnsrr() to validate email domains. The revamped version is listed below:
function checkEmail($email) { if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)){ list($username,$domain)=split('@',$email); if(!getmxrr ($domain,$mxhosts)){ return false; } return true; } return false; }
And we call it in the following manner:
$email = trim($_POST['email']); if(!checkEmail($email)) { echo 'Invalid email address!'; } else { echo 'Email address is valid'; }
This code looks very similar to the previous example using checkdnsrr(). The only subtle difference lies in that we have wrapped getmxrr() into the checkEmail() function. It’s a simple but powerful solution. As we can appreciate, the set of PHP network functions is an invaluable tool for validating email domains.
So far, we have defined individual functions to first, validate the proper format for an email address, and next, check whether the email domain is a real domain. From a strict point of view, this solution is still incomplete, because we really don’t know if the given user name is valid. How can this issue be addressed properly?
Actually, there is no direct way to do that. However, a fairly handy approach to help us see whether we are dealing with a valid user name might show us whether the email domain is currently in use. In that way, we can be somewhat more certain (but never completely) that someone is using that domain to send and receive email messages. This might brings us to the conclusion that the given user name is potentially valid. Let’s move to the next section to find out how we can determine this with a little extra work.
| | Discuss Email Address Verification with PHP | | | | | | | As described in the article, the whole function might be included as a
new method... | | | | | | Hello,
Nice but its not working for me. Its not getting any furter... | | | | | | Hello,
The regular expression seems to work fine. Did you try using another email... | | | | | | You should remove spaces from the regular expression, use this... | | | | | |
The pattern seems to work. Have you tried with other pattern?
Regards
... | | | | | | In section 6 the author opens a connection on the SMTP port directly to the domain... | | | | | | Hi,
The revamped version of the function is quite good. Good job.
| | | | | | Hi again,
When I initially wrote the email verification function, I just... | | | | | | here is the above code as a... | | | | | | However the class is overall well defined, email format verfification is redundant... | | | | | | can you show an example of the & usage you are describing. | | | | | | Ok. Using the given class example, you should instantiate the class in... | | | | | | I love the class and find it useful, but I have one problem.
If the domain isn't... | | | | | | I saw that too.
when calling the last part of the class try using the... | | | | | | Hi,
If you don't want to see the warnings generated by PHP functions, just use
... | | | | | | There are a lot of allowed characters for the local part of the email address that... | | | | | | Just thinking...
This verification would fail if the client uses an external... | | | | | | Hi all,
The same concept came to my mind about making sure username validity.... | | | | | | That's a good reference to the allowable characters for the firs part of an email... | | | | | | Firewalled or not, any server that accepts mail from any address on the Internet... | | | | | | Sure I know that Simple Mail Transport Protocol is for sending email. And
that's... | | | | | |
I point to myself, SMTP:Simple Mail Transfer Protocol :-)
| | | | | | The goal of email validation would be one of two things. First to catch typo's. ... | | | | | | I've read with interest your commnents about email address verification, and I think... | | | | | | the original regex in the article doesnt work whatsoever (returns false... | | | | | | Thank you for the useful feedback. According to what you point in the regexp, it... | | | | | | I agree completely that this method is very pointless. Sending an email to the user... | | | | | | Been following this with a heck of a lot of interest. Has the 'final' code been... | | | | | | I think someone should at least mention performance. The final solution can add... | | | | | | Hey all,
I've been looking into email verification and cannot seem to
find... | | | | | | Hello Matt,
Thank you for reading and commenting on my article. With reference to... | | | | | | Why do people write articles from the begining that is not in fuction?
Stupid! | | | | | | There's no need to post aggressive comments. If you don't like what you read, then... | | | | | | Happening to me too. | | | | | | Thanks for comenting on this article. I have two suggestions for this issue: first,... | | | | | | Can anyone paste the complete codes in here? I m blurred already with different... | | | | | | Hello,
Thank you for posting your message here. Now, below there's the complete... | | | | | | I just came across this now, I was searching for a way to validate emails in php as... | | | | | | Thank you for your useful comments regarding my article. Yeap, according to the... | | | | | | HI.
I have tried this script and other scripts. But usually I receive the same... | | | | | | Hi,
Thank you for commenting here. The socket connection error you're getting may... | | | | | | I think this validation overall is good in terms of showing the usability of some of... | | | | | | First off, I'd like to thank you for posting your comments here. Now, concerning... | | | | | | Am new to PHP programming, but am having a ball learning. Am in the early stages of... | | | | | | Hi Tom,
First off, I’d like to thank you for introducing your extensive comments... | | | | | | I was using a simple double "if" to check for the @ + . signs, but i wanted a more... | | | | | | Definitively, your reg expression seems to work better than the one shown in the... | | | | | | Come ON, DevShed!
I'm figuring the author MUST have tested the regex while... | | | | | | Thank you for your feedback on my article. Yeaph, you’re correct concerning the... | | | | | | Thanks to all of you for sharing your knowledge, it really is of great help. It... | | | | | | Thank you for commenting on my PHP article. Now, I’d suggest you the following... | | | | | | What about this additional function:function check ($host,$user){ $fp =... | | | | | | Hello Martin,
Thank you for posting your comments here. And with reference to... | | | | | | Hello Alejandro,
Thanks for your article.
This function isn't from me. I... | | | | | | Hi again Martin,
I'd like to thank you again for your kind comments on my PHP... | | | | | | Martin - thanks for sharing this with us. I've been looking in several places for... | | | | | | I like the double opt-in for first-time user registration - check email has valid... | | | | | | Hi Caroline,
Thanks for posting your detailed, yet easy-to-follow description on... | | | | | | ... | | | | | | Well, I’m afraid your comment is completely wrong. My email checking function... | | | | | | I hate it when someone is critical of the hard work of others. Even if your work... | | | | | | The following code: .[a-z]{2,4}) assumes the TLD is 2-4 characters. Already we have... | | | | | | I wrote this article more than four years ago, when those TLD didn’t even exist.... | | | | | | I found this page helpful, but nothing on it worked, so I wrote my own from all the... | | | | | | My function worked remarkably well, but you should consider the fact I wrote it over... | | | | | | >>> Post your comment now! | | | | | |
|
 |
|