Problem of checking email validity

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Problem of checking email validity

Post by rei27 »

Hey all,

I am facing a problem for my website. My website is a ecommerce website, it will generate some auto email to user. For example, when a user successful register, a email will send to him.

The problem is, some of my user give the wrong email. It causes my email function terminated by my hosting company(server side). Because they suspect my side keep send the spam.

We found out the problem is caused by the invalid email address, so is it any coding to check the email validity before i send out?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Problem of checking email validity

Post by manohoo »

Provide your definition of "valid email address" and then we will be able to help you. And please be detailed with the email format.
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Problem of checking email validity

Post by rei27 »

For example, i received a lot of email like:
abc@abc.com
cde@cde.com

This kind of email address are invalid, so the server will treat as spam!

I need the code to check where it is valid or not!

I am trying checkdnsrr(), but seem like failed to get what i want!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Problem of checking email validity

Post by Apollo »

rei27 wrote:For example, i received a lot of email like:
abc@abc.com
cde@cde.com
This kind of email address are invalid, so the server will treat as spam!
Well, abc.com and cde.com are both valid, existing domains. So what if the owner of abc.com (or whoever has the address abc@abc.com) wants to subscribe to your site?
Obviously, when someone fills in abc@abc.com it's a fake (well at least for 99.9999% sure), but you get the idea.

You can filter out malformatted addresses (like "a@a" or "no spam plz" or "@@@@" or whatever) with a simple regexp, and you can filter out bogus email addresses with non-existing domains (like "you@yer.momma" or "no@spam.plz") by resolving the domain. But to filter out valid yet suspicious addresses like abc@abc.com, you'd have to use some sort of blacklist.

Why not instead send them a confirmation email containing a verification link, from some noreply@yoursite.com address which automatically deletes everything it receives.
People with valid addresses will receive the confirmation message, and when they click the link you know their address is valid.
Bounces from invalid addresses will not bother you, as they'll bounce to your noreply address, nicely disappearing into the void.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Problem of checking email validity

Post by manohoo »

Very good post by Apollo. If you want to keep things simple I'd use the blacklist approach. Grab a list of bad domains, put it in your database or a text file, before sending the email validate it against the blacklist.

Since "bad" domains are created all the time, you will have to maintain the blacklist regularly.

Also, the following will help you get rid of some bad domains.

Code: Select all

// Resolve domain to ip:
<?php $domain = "yahoo.com";
if (gethostbyname($domain)== $domain)
  {echo "$domain is a bad domain";}
  else {echo "$domain is a good domain";}
?>
I wouldn't use the code above in production, just play with it to get an idea of what options are available.
Post Reply