bindashsandeep / php-smtp-email-validation

Automatically exported from code.google.com/p/php-smtp-email-validation
0 stars 0 forks source link

PHP SMTP Email Validation with MySQL support to update fields based on email OK or NOT OK? #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
NONE

What is the expected output? What do you see instead?
Variables needed to store these: $code 250, $code 200, etc...
I would like to know what they are or how to get them?
couldn't I just do this:

if ($code == '250') {
// you received 250 so the email address was accepted
$results[$user.'@'.$domain] = true;

// add update statement here??

} elseif ($code == '451' || $code == '452') {
// you received 451 so the email address was greylisted (or some temporary 
error occured on the MTA) - so assume is ok
$results[$user.'@'.$domain] = true;

// add update statement here??

} else {
$results[$user.'@'.$domain] = false;

// add delete statement here?? 

}

What version of the product are you using? On what operating system?
Latest version on my hosts linux box.

Please provide any additional information below.
need to validate email addresses, having that info will help me know what to 
keep and what to delete.

Original issue reported on code.google.com by kleynwes...@gmail.com on 11 Jun 2010 at 7:04

GoogleCodeExporter commented 9 years ago
I have recently come across this project at 
http://code.google.com/p/php-smtp-email-validation after working with 
http://code.google.com/p/php-email-address-validation for quite awhile.

I am currently working on php code to work with mysql to do, what I believe, is 
the same thing you are trying to accomplish.

My database columns would be something simple like:
| Customer_Name | Email_Address | Validation_Results |
I would query the database for the email address(s) and put them into an array.
I too would appreciate any help available to complete this project.

Thank you,
Terry Mullins

Original comment by terrymul...@gmail.com on 16 Oct 2010 at 1:52

GoogleCodeExporter commented 9 years ago
The current version r5 will take an array of emails, and return an associative 
array (hash) with the results. 

$results = $SMTP_Validator->validate(array('email1@example.com', 
'email2@example.com'));

You can iterate over the results and store in the db easily:

foreach($results as $email => $result) 
{
   // $email is the email and $result is a boolean true or false
}

If you want the response codes, you'll have to edit the class to return the 
results instead of a boolean true or false.

Original comment by buca...@gmail.com on 16 Oct 2010 at 3:53

GoogleCodeExporter commented 9 years ago
I'm not sure that I know how to download r5.  I have the Alpha version, I think.
I just want to save the results as a boolean in the database.

So I would query something like,

if ($conn)
{
    $fetch = mysql_query("SELECT email,results FROM customers"); 

    /* Retrieve and store in array the results of the query.*/

    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['email'] = $row['email'];
        $row_array['result'] = $row['result'];

        array_push($return_arr,$row_array);
    }

}

Does this look correct ?, or am I going about this in the wrong way ?

Original comment by terrymul...@gmail.com on 16 Oct 2010 at 4:09

GoogleCodeExporter commented 9 years ago
http://code.google.com/p/php-smtp-email-validation/source/detail?r=5

I think that I am starting to get it... I go to the above url and click on the 
links that have the php code & replace the original code in that corrosponding 
file with the new code found in the r5 ?

I didn't see any other examples listed that use more than 1 email in the 
array... did I miss something?

Original comment by terrymul...@gmail.com on 16 Oct 2010 at 5:08

GoogleCodeExporter commented 9 years ago
Hi Terry, 

You are not alone in this one.
I found a thread that's similar: 
http://www.codingforums.com/showthread.php?t=197449 

What do you think?

Original comment by kleynwes...@gmail.com on 16 Oct 2010 at 7:11

GoogleCodeExporter commented 9 years ago
All you really need is r3

http://php-smtp-email-validation.googlecode.com/svn-history/r3/trunk/smtp_valida
teEmail.class.php

If you're using windows then you will need r5 because it includes the pear 
Net_DNS class which allows PHP < 5.3 on windows to make DNS queries.

RE: Variables needed to store these: $code 250, $code 200, etc...
-----------------------------------------------------------------

I think the easiest way to return the codes instead of a boolean from the class 
is to remove the check for codes and replace it with:

$results[$user.'@'.$domain] = $code;

That way in the results you have the emails and associated codes. 

Then you can loop through them and do the queries you want based on the code.

RE: mysql queries
-----------------

Those queries look correct.

Original comment by buca...@gmail.com on 18 Oct 2010 at 2:38

GoogleCodeExporter commented 9 years ago
I am on a dedicated server running CentOs 5 which I recently updated, so I am 
using the latest versions of Php & MySql that this OS supports.

I just have a quick couple of questions.

I currently have the Alpha version... Do I just need to replace the 
smtp_validateEmail.class.php with the code referenced in the above link ? (or) 
Will I need to replace all of the files with the code listed in the r3 trunk ?

Is there any reason why this wouldn't work well if I made it part of the while 
control structure instead of making it a part of a foreach control structure ?

Thanks again.
I will post a working copy of this code, once complete.

Original comment by terrymul...@gmail.com on 20 Oct 2010 at 9:04

GoogleCodeExporter commented 9 years ago
Just replace smtp_validateEmail.class.php. 

There is not reason why 'while' would not work instead of 'foreach' if the 
logic is equivalent. 

Original comment by buca...@gmail.com on 21 Oct 2010 at 3:10

GoogleCodeExporter commented 9 years ago
Thanks for your assistance :D
I was able to get this to work on my server, however, after doing a batch of 
these for a few minutes I was getting codes back like:

421 4.7.0 [GL01] Message from (99.999.999.999) temporarily deferred - 4.16.50. 
Please refer to http://postmaster.yahoo.com/errors/postmaster-21.html  

btw I replaced my ip with 9's above...

Now that I know this, I would really like to put more information into my 
database such as the error code(s) and other text that tells us what to do to 
correct any problems, etc...

I have over 2 million emails that I need to validate, so I really need for this 
to run on it's own without too much user intervention.

I need a way to automatically stop checking, for instance @yahoo addresses, 
after receiving certain error codes back.

Here is the working code that I have so far.  

<?php
require ('../../connections/databases/customers.php');

/**
 * I modified Example 1 from R1 Alpha
 * Validate a single Email via SMTP
 * 
 * This now validates multiple Emails via SMTP from a MySql Database
 */

// include SMTP Email Validation Class

require_once('smtp_validateEmail.classR3.php');

error_reporting(E_ALL | E_STRICT);
   ini_set('display_errors', '1');

$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
if (mysql_error()) {
printf("Connect failed: %s\n",mysql_error());
exit ();
}
mysql_select_db($dbname);
if (mysql_error()) {
printf("Connect to database failed: %s\n",mysql_error());
exit ();
}

/* If connection to database, run sql statement. */
if ($conn)
{   
$query = sprintf("SELECT id,valid_syntax,email,yahoo,pass_smtp_test FROM 
customers WHERE valid_syntax ='Y' ");

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for 
debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { 
 //printf ("ID: %s  Email: %s", $row[0], $row["email"]);

 $email= $row["email"];
 $valid= $row["valid_syntax"];
 $record_id = $row["id"];
 $smtp_test= $row["pass_smtp_test"];

// the email to validate  **** $email variable is defined on line #50
// $email = 'terry@budgetrentasite.org';
// an optional sender
$sender = 'terry@fivestarcleaningplus.com';
// instantiate the class
$SMTP_Validator = new SMTP_validateEmail();
// turn on debugging if you want to view the SMTP transaction
$SMTP_Validator->debug = true;
// do the validation
$results = $SMTP_Validator->validate(array($email), $sender);
// view results
echo $email.' is '.($results[$email] ? 'valid' : 'invalid')."\n";

// send email? 
if ($results[$email]) { 

mysql_query("UPDATE customers SET pass_smtp_test = 'Y' WHERE id = 
'$record_id'") or die(mysql_error());

  //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\r\n"); // send email
} 
else {
  echo 'The email addresses you entered is not valid';
mysql_query("UPDATE customers SET pass_smtp_test = 'N' WHERE id = 
'$record_id'") or die(mysql_error());  
  }
}
mysql_free_result($result);
mysql_close($conn);
}
?>

Original comment by terrymul...@gmail.com on 21 Oct 2010 at 3:03

GoogleCodeExporter commented 9 years ago
You'll need to limit the number and slow the rate of validations. You can do 
this with a cron job, or deamon etc. 

Or by simpliy using sleep() between checks. 

Original comment by buca...@gmail.com on 21 Oct 2010 at 3:24

GoogleCodeExporter commented 9 years ago
Awesome idea !!

I think I can put it at the bottom of the where { ... }.
What do you think I should limit the number of validations per hour to avoid 
problems ??

Original comment by terrymul...@gmail.com on 21 Oct 2010 at 7:18

GoogleCodeExporter commented 9 years ago
Just to pass this on... I added the sleep() function at the bottom of my where 
{...} to 12 seconds.  This should give me 5 per minute or 300 per hour.
I set the Limit in my query to 900 so this should run for about 3 hours 
according to my figures.

I will check back with this after this time and see how goes things.

Terry Mullins
terry@budgetrentasite.com

Original comment by terrymul...@gmail.com on 21 Oct 2010 at 8:53

GoogleCodeExporter commented 9 years ago
How do I go about passing the variable, $code in the function so that I can 
write it to the database ?

I would also like to use the $code variable avoid spamming the server so I 
would like to use it to stop searching in certain domains on certain error 
code(s).

Thanks again.

Original comment by terrymul...@gmail.com on 23 Oct 2010 at 4:19

GoogleCodeExporter commented 9 years ago
To return the code. 

Remove all of this:

if ($code == '250') {
// you received 250 so the email address was accepted
$results[$user.'@'.$domain] = true;

// add update statement here??

} elseif ($code == '451' || $code == '452') {
// you received 451 so the email address was greylisted (or some temporary 
error occured on the MTA) - so assume is ok
$results[$user.'@'.$domain] = true;

// add update statement here??

} else {
$results[$user.'@'.$domain] = false;

// add delete statement here?? 

}

Replace it with.

$results[$user.'@'.$domain] = $code;

Original comment by buca...@gmail.com on 23 Oct 2010 at 6:31

GoogleCodeExporter commented 9 years ago
Ok, so the $results will be either a boolean value ( true / false )
-or-
The code # ?

How can I go about doing both ?

Original comment by terrymul...@gmail.com on 23 Oct 2010 at 7:02

GoogleCodeExporter commented 9 years ago
You could do:

$results[$user.'@'.$domain] = array($boolean, $code);

Where $boolean is the boolean value.

Original comment by buca...@gmail.com on 23 Oct 2010 at 7:14