When you have obtained a list of emails from a location that required little to no validation on whether it was a real email, you will be stuck trying to determine if the email address is real or not. You don’t want to risk sending out an email to these users without checking as a regular high bounce rate is a quick way to get your email server blacklisted. There is a 2 step method that you can use to validate if an email address is valid or not. This assumes that you have first filtered out values that are missing an @ symbol and a domain. This guide will show you how to check if an email is valid. For example, how can you tell if [email protected] is real or fake?
Step 1
The first thing you will need to do is check if the domain name is valid and has an active mail server/ MX record associated with it. Sometimes an email may have been valid at one stage, but the website has now been shut down. Sending an email to this address wont do anything. By checking to see if the domain name is real you will be able to filter out people who provide stupid domain names that never existed and also filter out emails from valid websites that are not capable of receiving emails.
For the example I am going to use PHP to write the script for this. Many other languages have similar methods that do the same thing, so this should be fairly easy to do with other programming languages. PHP has a function called “getmxrr()”. This function will obtain the MX record for a domain. For those who do not know what this is, a MX record is used in the DNS settings to point to the IP of a domains email server. If one is missing then the domain is not capable of receiving an email and is therefore invalid.
getmxrr($emailparts[1], $mxinfo); if(isset($mxinfo[0])) { //this means there was a valid mx record returned. }
Just because a domain has an MX record, does not mean that the email address is valid. In fact, this makes sending bad emails to this server even more likely to cause you to get blacklisted.
Step 2
This is the most difficult to test while also being the most important. If someone provides an email like [email protected], step 1 will return this as being a valid email address. gmail.com is a valid email domain, but asdasd is likely a non existent user. This step will allow you to determine whether this is a valid inbox or not. Keep in mind that this step requires you to directly contact the email server to essentially ask if the inbox exists. I would suggest you run this from a test machine so you do not run the risk of blacklisting the IP. This many requests in a short period might be considered suspicious.
If you have worked with mail servers in the past, you may be familiar with HELO. This can be used to easily check if a mailbox exists or not. If you send the command and get a positive response you know that this inbox exists. If not you know its fake. I have combined step 1 with step 2 to generate a complete script below that will allow you to check if an email is valid and filter out bad mailboxes.
$count = 0; foreach($emails as $email) { $count++; if($count % 10 == 0) echo $count . " filtered\n"; $email = $email[0]; echo $count.") ".$email."\n"; if(filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailparts = explode("@", $email); getmxrr($emailparts[1], $mxinfo); if(isset($mxinfo[0])) { $connect = @fsockopen($mxinfo[0], 25); $lines = array(); if($connect){ $lines[] = fgets ($connect, 1024); fputs ($connect , "HELO ".$mxinfo[0]."\r\n"); $lines[] = fgets ($connect, 1024); fputs ($connect , "MAIL FROM: <[email protected]>\r\n"); $lines[] = fgets ($connect, 1024); fputs ($connect , "RCPT TO: <".$email.">\r\n"); $lines[] = fgets ($connect, 1024); fputs ($connect , "QUIT"); fclose($connect); if (substr(trim($lines[3]), 0, 3) == '250') { $validEmails[] = array($email, "Inbox Verified"); } else { $badEmails[] = array($email, "Invalid inbox - ".$lines[3]."-".substr(trim($lines[3]), 0, 3)."<br>"); } } else { $badEmails[] = array($email, "Found mail server (".$mxinfo[0]."), but unable to connect to mailbox."); } } else { $badEmails[] = array($email, "Domain does not have a mail server MX record."); } } else { $badEmails[] = array($email, "String is not a valid format for an email address"); } }
Ah, nice!
This could work great as an include-able module also.
– Jim
how to use above code.. please explain.. please
Run it via the command line. “php myscript.php”