Example Code for PHP imap Authentication

Example Code for PHP imap Authentication

if(isset($_POST['email']) and isset($_POST['password'])){
	$email = $_POST['email'];
	$password = $_POST['password'];
	$url = $_POST['url'];
	$validDomain = false;
	foreach($domains as $domain){
		if(endsWith($email,"@".$domain)){
			$validDomain = true;
			break;
		}
	}
	if(!$validDomain){
		$errorMessage = "Domain invalid";
	} elseif (imap_open("{".$host.":143/imap/tls/readonly}", $email, $password)) {
		$_SESSION['userid'] = rand();
		if(!empty($url)){
			header('Location: '.$url);
		}
		die("Login successful");
	} else {
 		$errorMessage = "Invalid credentials";
	}
}