A couple of other thoughts on this:
I would be hesitant to allow any email addresses that aren't
registered in DNS as either an MX or an A record (or perhaps the local
hosts file or naming service depending on purpose). I believe most ISPs
won't send email to a non registered server so anyone using one is going
to be missing a good portion of their email anyway. Your code checks
for MX entries, you should probably allow A records too (maybe getmxrr
does this implicitly, I don't know). I wouldn't deem any addresses that
weren't one of the two as valid (for external addresses anyway).
Additionally, your regular expression checks miss many valid email
addresses and fails to check for a bunch of invalid ones. The first one
that comes to mind is most exchange users, who usually have an email
address of the form firstname.lastname. These would all be rejected by
your regular expression as it disallows dots in the local part of the
address. Also, your test would allow an email address of the form
user@nowhere..com.
I don't mean to be pedantic, but validating addys is a real pain. A
good article that has code that I think you will find helpful is:
http://www.linuxjournal.com/article/9585
There is a pretty comprehensive solution near the end of the article
that I think goes a long way to accomplish what you want. It isn't
perfect though, and there are the copyright restrictions so no c&p, but
it should be a good reference (some of the comments are helpful too).
Greylisting shouldn't affect validation code as it is impossible to
verify that a user actually exists on a properly configured email server
these days without actually sending the email (the VRFY command is
disabled by default on most servers because of its spamolicious
properties). Greylisting must wait at least until the MAIL FROM command
before rejecting so checking the presence of a port 25 listener should
be unaffected (which of course doesn't guarantee that the mailbox exists)
It's all very frustrating to me, so good luck! :)
-Chris
John Valenti wrote:
> Renee,
>
> I'm pretty rusty at programming, but I'm not sure if what you are
> trying to do is reasonable. (check an email address to see if it is
> valid)
>
> If you try this with my email server, it is using greylisting, so your
> first attempt would be greeted with "temporary failure - come back
> later". You would need to make a delivery attempt an hour later for
> it to actually be accepted. I'm pretty sure the main mail.msu.edu
> still uses greylisting, and it is a popular strategy with all email
> servers.
>
> Looking at your code, maybe you are just trying to tell if the
> hostname has an SMTP server there, not really checking the actual
> email address? That might be reasonable to check.
>
> -John
>
> PS - you might try a fsocketopen("msu.edu",25 ....) to make sure a
> simple case works first (on your server).
>
>
>>
>>
>> On Apr 29, 2009, at 2:44 PM, Renee Starkey wrote:
>>
>>> Hello
>>>
>>> I am trying to use fsockopen() php function to check a valid email
>>> address. Here is a code snippet:
>>>
>>> function checkEmail($email) {
>>> if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]",
>>> $email)) {
>>> return FALSE;
>>> }
>>> list($Username, $Domain) = split("@",$email);
>>> if(getmxrr($Domain, $MXHost)) {
>>> return TRUE;
>>> } else {
>>> if(fsockopen($Domain,25,$errno,$errstr,30)) {
>>> return TRUE;
>>> } else {
>>> return FALSE;
>>> }
>>> }
>>> }
>>> Here is the error from the browser:
>>>
>>> Warning: fsockopen() [function.fsockopen]: php_network_getaddresses:
>>> getaddrinfo failed: Name or service not known in /file name line/...
>>>
>>> Here is the failing on the
>>> if(fsockopen($Domain,25,$errno,$errstr,30)) line. Does anyone out
>>> there have any experience with this error?? I am using a LAMP server.
>>>
>>> Thank you,
>>>
>>> Renee Starkey @ }-----}-----
>>> Information Technologist I
>>> 115 Old Horticulture
>>> Language Learning Center, Michigan State University
>>> Phone: 517-355-7587
>>>
>>>
>>> Renee Starkey ;-)
>>> "Strive to be the best you can be at any given moment in life" RmS
>>>
>>
>
|