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

Need Help Choosing a New GPU After My 4090 Went Bad

Hey everyone, I'm in a bit of a situation. My Nvidia 4090 has developed some VRAM issues and unfortunately, it's out of warranty, so...

Looking for Python Practice Problems to Boost My Skills

I've been learning Python and am finally getting the hang of the basics like loops, functions, and lists. However, I'm feeling trapped in what's...

Is O(c^(k+1)) the same as O(c^k)?

I'm working on a project related to the Hitting Set problem, which involves exponential algorithms, and I stumbled upon a question: Are O(c^(k+1)) and...

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

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

Latest Posts

Latest Questions