• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Email Bounce Cleaner

I need code to make an email bounce cleaner that will select the Email addresses from DB table1 and check each one to see if it is a valid email address (by which it doesnt bounce or if the dotcom doesnt exist) and if it is, the to copy to a new table2.

Can this be done? If so how .. is there any sample php code i can use?
0
Sam Cohen
Asked:
Sam Cohen
1 Solution
 
Vel EousResearch & Development ManagerCommented:
How about something like:

(Email validation script from - http://www.linuxjournal.com/article/9585)
<?PHP
 
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 ªcheckdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}
 
 
$all_addresses = mysql_query ( "SELECT * FROM table1" )
	or die ( "Could not retriev data: " . mysql_error() );
	
while ( $row = mysql_fetch_assoc ( $all_addresses ) )
{
	$curr_address = $row['address'];
	if ( validEmail ( $curr_address ) === true )
	{
		$add_valid_address = mysql_query ( "INERT INTO table2 address VALUES '$curr_address'" );
	}
}
 
?>

Open in new window

0
 
Sam CohenAuthor Commented:
i will try this.. =)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now