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

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

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...

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

Markdown To Html Converter

Welcome to our web-based tool designed to make your life easier by converting Markdown to HTML in a matter of seconds. Our user-friendly interface...

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Latest Posts

Latest Questions