Test SMTP ports connection from PHP server

Upload this script in php file in root of web server:

<?php
		
// IMPORTANT NOTE - This script is for troubleshooting purposes only, it should *NOT* be used in production code and should be removed from your webserver once you have finished troubleshooting.
// 
// DESCRIPTION - This script will test DNS resolution, connectivity, port availability and SMTP credentials with the AuthSMTP network.
//
// INSTRUCTION - Please update the username and password below, upload this script to your webserver and then go to the page in a browser.


$smtp_user = "contact@rdcc.ro"; // ACTION REQUIRED - Replace with your AuthSMTP username (acXXXXX)
$smtp_pass = "eeshah8iN1fu"; // ACTION REQUIRED - Replace with your AuthSMTP SMTP password
$plain_text = false; // We will not use plain text authentication because it is insecure, true = use plain text (AUTH LOGIN)

// !!!!!!! -------- Please do not edit below this line -------- !!!!!!!

?>


<!DOCTYPE html>
<html>
	<head>
		<title>AuthSMTP - Diagnostics</title>
		
		<style>
			
			html{
				font-family: sans-serif;
			}
			
			.green{
				color:#3e9807;
			}
			
			.red{
				color:#dc0b0b;
			}
			
			.orange{
				color:#ff9200;
			}
			
			.blue{
				color:#0c73ef;
			}
			
		</style>
	</head>
	<body>
		
		<?php
		
		// ==== Set some default values ====
		$smtp_user_b64 = base64_encode($smtp_user);
		$smtp_pass_b64 = base64_encode($smtp_pass);
		
		$smtp_host	= "mail.mkdata.ro";
		$smtp_ip	= gethostbyname($smtp_host);
		$smtp_ports	= array("25","80","587","465");
		$available_ports = array();
		
		
		// ==== Test DNS resolution / connectivity ====
		//
		echo("<h3>======== Testing DNS resolution and connectivity ========</h3>");

		$test_dns = gethostbyname($smtp_host);
		
		if( preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $test_dns ) ){
			
			echo("<p>Correctly resolved SMTP hostname (".$smtp_host.")</p>");

			// ==== Try pinging the network (may not be reliable) ====
			//
			exec("ping -c 1 ".$smtp_host, $output, $retval);

			if( !isset($output[1]) ){
				
				$output[1] = ""; // If not result just add a fake value so we can evaluate something.
			
			}

			
			// ==== Check if we got a valid ping response ====
			// 
			if(	preg_match('/[0-9]{2}\sbytes\sfrom\smail\.authsmtp\.com\s\(([0-9\.]{11,15})\):\sicmp_seq=1\sttl=[0-9]{1,2}\stime=([0-9\.]{1,6})\sms/', $output[1], $results ) ){
			
				echo ("<p>Successfully pinged ".$smtp_host." in ".$results[2]." milliseconds.</p>");
				
			}else{
				
				echo ("<pCould not ping ".$smtp_host." - this may indicate a connectivity issue with your hosting account but could so just mean you don't have access to the ping command or pings may be blocked - we will continue anyway...</p>");
				
			}
					
		}else{
			
			exit("<p>Unable to resolve SMTP hostname (".$smtp_host.") - you appear to have a DNS resolution issue with your hosting account.</p>");
			
		}
		

		
		// ==== Test SMTP Ports ====
		//
		echo("<h3>======== Testing available SMTP ports ========</h3>");

		foreach( $smtp_ports as $port ){
			
			if( $socket = fsockopen($smtp_host,$port,$errno,$errstr,2) ){
				echo ("<p>Port ".$port." is open</p>");
				fclose($socket);
				$available_ports[] = $port;
				flush();
			}else{
				echo ("<p>Port ".$port." is NOT open (".$errno." / ".$errstr.")</p>");
				flush();
			}
			
			unset($socket);
		}
		

		// ==== If we have no available ports exit ====
		//
		if( count($available_ports) == 0 ){
			
			exit("<p class=\"red\">None of the SMTP ports that we provide are open on your hosting account - please contact your hosting provider and request that they open one of the ports listed above.</p>");
			
		}
		
		
		echo("<h3>======== Testing your SMTP username and password ========</h3>");
		

		// ==== Open a connection to test SMTP credentials ====
		//
		if( $socket = fsockopen($smtp_host,$available_ports[0],$errno,$errstr,2) ){
			
			echo("<p>Opened connection to ".$smtp_host." on port ".$available_ports[0]."</p>");
			flush();
			
		}else{
			
			exit("<p class=\"red\">Could not open connection to ".$smtp_host." on port ".$available_ports[0]."</p>");
			
		}
		

		// ==== Receive helo and check we are not being proxied ====
		// 
		$buffer1 = fgets($socket);
		
		if( preg_match('/^220\smail\.authsmtp\.com/', $buffer1) ){
			
			echo("<p>Received helo from ".$smtp_host." on port ".$available_ports[0]."</p>");
			
		}else{
			
			echo("<p class=\"red\">RECEIVED: ".$buffer1."</p>");
			exit("<p class=\"red\">Unexpected response from ".$smtp_host." on port ".$available_ports[0]." (was expecting helo)</p>");
			
		}
		

		// ==== Send EHLO ====
		// 
		$next_string = "EHLO test.example.com";
	
		if( fwrite($socket, $next_string."\r\n") ){
			echo("<p>WRITE: ".$next_string."</p>");
			flush();
		}else{
			exit("<p class=\"red\">WRITE: ".$next_string." (FAIlED/EXITING)</p>");
		}
		
		
		// ==== Receive welcome banner ====
		// 
		if( $buffer1 = stream_get_line($socket, 2048, "HELP") ){
			
			echo("<p>Received welcome banner response from ".$smtp_host." on port ".$available_ports[0]."</p>");
			
		}else{
			
			exit("<p class=\"red\">Unexpected response from ".$smtp_host." on port ".$available_ports[0]." (was expecting welcome banner)</p>");
			
		}
		
		
		// ==== Using hashed password ====
		// 
		if( $plain_text === false ){
			
			$algos = hash_algos();
		
			if( !in_array("md5", $algos) ){ // Check the MD5 algorithm is available
				
				exit("<p class=\"red\">The MD5 hashing algorithm is not available on this hosting service - unable to securely test SMTP authentication, to enable plain text authentication please set the variable 'plain_text' to 'true' at the start of the script.</p>");

			}else{

				echo("<p>Securely testing SMTP credentials with <b>hashed password</b> authentication.</p>");

			}
		

			// === Send AUTH CRAM-MD5 ====
			// 
			$next_string = "AUTH CRAM-MD5";
		
			if( fwrite($socket, $next_string."\r\n") ){

				echo("<p>WRITE: ".$next_string."</p>");

			}else{

				exit("<p class=\"red\">WRITE: ".$next_string." (FAIlED/EXITING)</p>");

			}

			
			// ==== Receive MD5 Challenge ====
			//
			$buffer1 = fgets($socket); // Not actually used for anything
			$buffer2 = fgets($socket);


			// ==== Build MD5 Challenge Response ====
			// 
			$md5_chal = explode(" ", $buffer2);
			
			if( $md5_chal[0] == "334" ){
				
				echo("<p>Received MD5 challenge from ".$smtp_host." on port ".$available_ports[0]."</p>");
				
				$md5_pass = hash_hmac( 'md5', base64_decode($md5_chal[1]), $smtp_pass );
				$md5_resp = base64_encode($smtp_user." ".$md5_pass);
				
			}else{
				
				exit("<p class=\"red\">Unexpected response from ".$smtp_host." on port ".$available_ports[0]." (was expecting MD5 challenge)</p>");
				
			}

			
			// ==== Send MD5 Challenge Response ====
			// 
			$next_string = $md5_resp;
		
			if( fwrite($socket, $next_string."\r\n") ){
				echo("<p>WRITE: MD5 Challenge Response</p>");
			}else{
				exit("<p class=\"red\">WRITE: MD5 Challenge Response (FAIlED/EXITING)</p>");
			}
			

			// ==== Evaluate the response ====
			// 
			$buffer1 = fgets($socket);
			
			if( preg_match('/235\s2\.0\.0\sOK\sAuthenticated/', $buffer1) ){
				
				echo("<p class=\"green\">SUCCESS - Username and password are correct.</p>");
				
			}elseif( preg_match('/535\s5\.7\.0\sauthentication\sfailed/', $buffer1) ){
				
				echo("<p class=\"red\">FAIL - Username and / or password are incorrect.</p>");
				echo("<p class=\"blue\">HELP - If you are unsure of your AuthSMTP SMTP password you can reset it via the 'SMTP Settings' page in the <a href=\"https://www.authsmtp.com/login/\">control panel</a></p>");
				
			}else{
				
				echo("<p class=\"red\">ERROR - Received unexpected response - '".$buffer1."'.</p>");
				
			}

			
		}
			
		// Using plain text authentication
		// 
		if( $plain_text === true ){
			
			echo("<p>Testing SMTP credentials with <b>plain text</b> authentication.</p>");
			
			// === Send AUTH LOGIN ====
			// 
			$next_string = "AUTH LOGIN";
		
			if( fwrite($socket, $next_string."\r\n") ){
				echo("<p>WRITE: ".$next_string."</p>");
			}else{
				exit("<p class=\"red\">WRITE: ".$next_string." (FAIlED/EXITING)</p>");
			}

			
			// ==== Receive welcome banner ====
			// 
			$buffer1 = fgets($socket); // Not actually used for anything
			$buffer2 = fgets($socket);
			
			
			if( preg_match('/Username:/', base64_decode(substr($buffer2, 4)) ) ){
				
				echo("<p>Received username prompt from ".$smtp_host." on port ".$available_ports[0]."</p>");
				
			}else{
				
				exit("<p class=\"red\">Unexpected response from ".$smtp_host." on port ".$available_ports[0]." (was expecting username prompt)</p>");
				
			}

			
			// === Send Username ====
			// 
			$next_string = $smtp_user_b64;
		
			if( fwrite($socket, $next_string."\r\n") ){
				echo("<p>WRITE: Encoded username</p>");
			}else{
				exit("<p class=\"red\">WRITE: Encoded username (FAIlED/EXITING)</p>");
			}
			
			$buffer1 = fgets($socket);
			
			if( preg_match('/Password:/', base64_decode(substr($buffer1, 4)) ) ){
				
				echo("<p>Received password response from ".$smtp_host." on port ".$available_ports[0]."</p>");
				
			}else{
				
				exit("<p class=\"red\">Unexpected response from ".$smtp_host." on port ".$available_ports[0]." (was expecting password prompt)</p>");
				
			}

		
			// === Send Password ====
			// 
			$next_string = $smtp_pass_b64;
		
			if( fwrite($socket, $next_string."\r\n") ){
				echo("<p>WRITE: Encoded password</p>");
			}else{
				exit("<p class=\"red\">WRITE: Encoded password (FAIlED/EXITING)</p>");
			}
			
			$buffer1 = fgets($socket);
			
			if( preg_match('/235\s2\.0\.0\sOK\sAuthenticated/', $buffer1) ){
				
				echo("<p class=\"green\">SUCCESS - Username and password are correct.</p>");
				
			}elseif( preg_match('/535\s5\.7\.0\sauthentication\sfailed/', $buffer1) ){
				
				echo("<p class=\"red\">FAIL - Username and /or password are incorrect.</p>");
				echo("<p class=\"blue\">HELP - If you are unsure of your AuthSMTP SMTP password you can reset it via the 'SMTP Settings' page in the <a href=\"https://www.authsmtp.com/login/\">control panel</a></p>");
				
			}else{
				
				echo("<p class=\"red\">ERROR - Received unexpected response - '".$buffer1."'.</p>");
				
			}
			
		}
			
						
		fclose($socket);
		
		?>
		<br>
		<br>
		<p class="orange"><b>IMPORTANT: Don't forget to remove the script from your server once you have finished!</b>
	</body>
</html>