Array

How To Check If An Email Is Valid And Inbox Exists

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");
	}
}

 

Related Articles

Related Questions

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

Whats the difference between the Tapo P100 and the P105?

There are a few different Tapo smart plugs. The P100 and P110 differ based on the smart power monitoring feature but where does the...

Time remaining keeps going up on Whirlpool dryer??

I have clothes in the dryer regularly and the time remaining shows on the display. There could be 10 mins remaining and if I...

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Bitrate Converter

Below you will find a bitrate converter. This tool will allow you to enter a bitrate value, in one of many different formats and...

Aesthetic Text Generator

There are various ways to make your social media profile seem more unique, some of which are not as easy to implement as others....

Aspect Ratio Calculator For Images

Aspect ratio is the ratio between the height and width of an image. If you want to resize an image by 100 pixels, you...

Add Text To Image

Use this free tool to add text to an image. Simply select the image file that you want to overlay text onto and you...

JavaScript Multi-line String Builder

Javascript did not always support multi-line strings. If you attempted to create a string variable using quotes, putting a line break into the source...

GUID Generator

GUID generation is relatively simple. It is a system that is used to generate unique values without having to keep track of the IDs...

Latest Posts

Latest Questions