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

Is Max’s Context Window Larger than 200k?

I'm considering buying Max, but I'm concerned about its 200k context window length, especially when compared to Gemini. Is Max planning to offer a...

Will a Hacker’s Access Show Up in My Account Activity Logs?

I've been thinking about security after learning about session hijacking. Suppose I'm using my computer and a hacker steals my cookies and session ID....

How do I find the right BIOS update for my MSI B450M Bazooka Plus?

I'm working on a build with my MSI B450M Bazooka Plus motherboard and I'm stuck trying to find the correct BIOS update. I've been...

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

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions