Skip to content

Instantly share code, notes, and snippets.

@narfbg
Created June 7, 2012 09:30
Show Gist options
  • Save narfbg/2887855 to your computer and use it in GitHub Desktop.
Save narfbg/2887855 to your computer and use it in GitHub Desktop.
CI_Email::valid_email() hostname validation
diff --git a/system/libraries/Email.php b/system/libraries/Email.php
index c70144f..8968a84 100644
--- a/system/libraries/Email.php
+++ b/system/libraries/Email.php
@@ -704,11 +704,37 @@ class CI_Email {
* Email Validation
*
* @param string
+ * @param mixed types of records to check
* @return bool
*/
- public function valid_email($address)
+ public function valid_email($address, $options = FALSE)
{
- return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $address);
+ if ( ! preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $address))
+ {
+ return FALSE;
+ }
+ elseif (empty($options) OR ! function_exists('checkdnsrr')) // Doesn't exist under Windows prior to PHP 5.3
+ {
+ return TRUE;
+ }
+
+ $options = strtoupper($options);
+ sscanf($address, '%s@%s', &$name, &$host);
+
+ if ($options === 'MX' && ! checkdnsrr($host, $options))
+ {
+ return FALSE;
+ }
+ elseif (CI_Input::valid_ip($host))
+ {
+ return TRUE;
+ }
+ elseif (in_array($options, array('A', 'AAAA'), TRUE))
+ {
+ return checkdnsrr($address, 'A') OR checkdnsrr($address, 'AAAA');
+ }
+
+ return checkdnsrr($address);
}
// --------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment