def validate_email(email, check_mx=False, verify=False):
 try:
  assert (re.match(VALID_ADDRESS_REGEXP, email) is not None)
  check_mx |= verify
  if check_mx:
   if (not DNS):
    raise Exception('For check the mx records or check if the email exists you must have installed pyDNS python package')
   DNS.DiscoverNameServers()
   hostname = email[(email.find('@') + 1):]
   mx_hosts = DNS.mxlookup(hostname)
   for mx in mx_hosts:
    try:
     smtp = smtplib.SMTP()
     smtp.connect(mx[1])
     if (not verify):
      return True
     (status, _) = smtp.helo()
     if (status != 250):
      continue
     smtp.mail('')
     (status, _) = smtp.rcpt(email)
     if (status != 250):
      return False
     break
    except smtplib.SMTPServerDisconnected:
     break
    except smtplib.SMTPConnectError:
     continue
 except (AssertionError, ServerError):
  return False
 return True

def validate_email ( email , check_mx = False , verify = False ) : try : assert ( re . match ( VALID_ADDRESS_REGEXP , email ) is not None ) check_mx |= verify if check_mx : if ( not DNS ) : raise Exception ( 'For check the mx records or check if the email exists you must have installed pyDNS python package' ) DNS . DiscoverNameServers ( ) hostname = email [ ( email . find ( '@' ) + 1 ) : ] mx_hosts = DNS . mxlookup ( hostname ) for mx in mx_hosts : try : smtp = smtplib . SMTP ( ) smtp . connect ( mx [ 1 ] ) if ( not verify ) : return True ( status , _ ) = smtp . helo ( ) if ( status != 250 ) : continue smtp . mail ( '' ) ( status , _ ) = smtp . rcpt ( email ) if ( status != 250 ) : return False break except smtplib . SMTPServerDisconnected : break except smtplib . SMTPConnectError : continue except ( AssertionError , ServerError ) : return False return True
indicate whether the given string is a valid email address according to the addr-spec portion of rfc 2822 .