sepehr
8/13/2012 - 12:00 PM

PHP: Email Validation (RFC 2822)

PHP: Email Validation (RFC 2822)

/**
 * Verifies the syntax of the given e-mail address.
 *
 * See RFC 2822 for details.
 *
 * @param $mail
 *   A string containing an e-mail address.
 *
 * @return
 *   1 if the email address is valid, 0 if it is invalid or empty, and FALSE if
 *   there is an input error (such as passing in an array instead of a string).
 */
function valid_email_address($mail)
{
	$user   = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
	$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
	$ipv4   = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
	$ipv6   = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';

	return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
}