forked from syrusakbary/validate_email
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 49034b1
Showing
8 changed files
with
881 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.pyc | ||
build | ||
dist | ||
MANIFEST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
validate_email was created by Syrus Akbary <[email protected]> in | ||
April 2012. | ||
This package is based on the work of Noel Bush <[email protected]> | ||
https://github.com/noelbush/py_email_validation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Thanks for downloading validate_email. | ||
|
||
To install it, make sure you have Python 2.2 or greater installed. Then run | ||
this command from the command prompt: | ||
|
||
python setup.py install |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include AUTHORS | ||
include LICENSE | ||
include README.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
============== | ||
validate_email | ||
============== | ||
|
||
validate_email is a package for Python that check if an email is valid, properly formatted and really exists (connecting to the server and "asking") | ||
|
||
|
||
|
||
INSTALLATION | ||
============ | ||
|
||
First, you must do:: | ||
|
||
pip install validate_email | ||
|
||
Extra | ||
------ | ||
|
||
For check the domain mx and verify email exits you must have the `pyDNS` package installed:: | ||
|
||
pip install pyDNS | ||
|
||
|
||
USAGE | ||
===== | ||
|
||
Basic usage:: | ||
|
||
from validate_email import validate_email | ||
is_valid = validate_email('[email protected]') | ||
|
||
|
||
Checking domain has SMTP Server | ||
------------------------------- | ||
|
||
For check if the host has SMPT Server | ||
|
||
from validate_email import validate_email | ||
is_valid = validate_email('[email protected]',mx=True) | ||
|
||
|
||
Verify email exists | ||
------------------- | ||
|
||
Check if the host has SMPT Server and the email exists in the server. | ||
|
||
from validate_email import validate_email | ||
is_valid = validate_email('[email protected]',verify=True) | ||
|
||
|
||
TODOs and BUGS | ||
============== | ||
See: http://github.com/syrusakbary/validate_email/issues |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from setuptools import setup,find_packages | ||
|
||
setup(name='validate_email', | ||
version = '1.0', | ||
download_url = '[email protected]:syrusakbary/validate_email.git', | ||
py_modules = 'validate_email', | ||
author = 'Syrus Akbary', | ||
author_email = '[email protected]', | ||
description = 'Validate_email verify if an email address is valid and really exists.', | ||
long_description=open('README.rst').read(), | ||
keywords = 'email validation verification mx verify', | ||
url = 'http://github.com/syrusakbary/validate_email', | ||
license = 'LGPL', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# RFC 2822 - style email validation for Python | ||
# (c) 2012 Syrus Akbary <[email protected]> | ||
# Extended from (c) 2011 Noel Bush <[email protected]> | ||
# for support of mx and user check | ||
# This code is made available to you under the GNU LGPL v3. | ||
# | ||
# This module provides a single method, valid_email_address(), | ||
# which returns True or False to indicate whether a given address | ||
# is valid according to the 'addr-spec' part of the specification | ||
# given in RFC 2822. Ideally, we would like to find this | ||
# in some other library, already thoroughly tested and well- | ||
# maintained. The standard Python library email.utils | ||
# contains a parse_addr() function, but it is not sufficient | ||
# to detect many malformed addresses. | ||
# | ||
# This implementation aims to be faithful to the RFC, with the | ||
# exception of a circular definition (see comments below), and | ||
# with the omission of the pattern components marked as "obsolete". | ||
|
||
import re | ||
import smtplib | ||
import socket | ||
|
||
try: | ||
import DNS | ||
ServerError = DNS.ServerError | ||
except: | ||
DNS = None | ||
class ServerError(Exception): pass | ||
# All we are really doing is comparing the input string to one | ||
# gigantic regular expression. But building that regexp, and | ||
# ensuring its correctness, is made much easier by assembling it | ||
# from the "tokens" defined by the RFC. Each of these tokens is | ||
# tested in the accompanying unit test file. | ||
# | ||
# The section of RFC 2822 from which each pattern component is | ||
# derived is given in an accompanying comment. | ||
# | ||
# (To make things simple, every string below is given as 'raw', | ||
# even when it's not strictly necessary. This way we don't forget | ||
# when it is necessary.) | ||
# | ||
WSP = r'[ \t]' # see 2.2.2. Structured Header Field Bodies | ||
CRLF = r'(?:\r\n)' # see 2.2.3. Long Header Fields | ||
NO_WS_CTL = r'\x01-\x08\x0b\x0c\x0f-\x1f\x7f' # see 3.2.1. Primitive Tokens | ||
QUOTED_PAIR = r'(?:\\.)' # see 3.2.2. Quoted characters | ||
FWS = r'(?:(?:' + WSP + r'*' + CRLF + r')?' + \ | ||
WSP + r'+)' # see 3.2.3. Folding white space and comments | ||
CTEXT = r'[' + NO_WS_CTL + \ | ||
r'\x21-\x27\x2a-\x5b\x5d-\x7e]' # see 3.2.3 | ||
CCONTENT = r'(?:' + CTEXT + r'|' + \ | ||
QUOTED_PAIR + r')' # see 3.2.3 (NB: The RFC includes COMMENT here | ||
# as well, but that would be circular.) | ||
COMMENT = r'\((?:' + FWS + r'?' + CCONTENT + \ | ||
r')*' + FWS + r'?\)' # see 3.2.3 | ||
CFWS = r'(?:' + FWS + r'?' + COMMENT + ')*(?:' + \ | ||
FWS + '?' + COMMENT + '|' + FWS + ')' # see 3.2.3 | ||
ATEXT = r'[\w!#$%&\'\*\+\-/=\?\^`\{\|\}~]' # see 3.2.4. Atom | ||
ATOM = CFWS + r'?' + ATEXT + r'+' + CFWS + r'?' # see 3.2.4 | ||
DOT_ATOM_TEXT = ATEXT + r'+(?:\.' + ATEXT + r'+)*' # see 3.2.4 | ||
DOT_ATOM = CFWS + r'?' + DOT_ATOM_TEXT + CFWS + r'?' # see 3.2.4 | ||
QTEXT = r'[' + NO_WS_CTL + \ | ||
r'\x21\x23-\x5b\x5d-\x7e]' # see 3.2.5. Quoted strings | ||
QCONTENT = r'(?:' + QTEXT + r'|' + \ | ||
QUOTED_PAIR + r')' # see 3.2.5 | ||
QUOTED_STRING = CFWS + r'?' + r'"(?:' + FWS + \ | ||
r'?' + QCONTENT + r')*' + FWS + \ | ||
r'?' + r'"' + CFWS + r'?' | ||
LOCAL_PART = r'(?:' + DOT_ATOM + r'|' + \ | ||
QUOTED_STRING + r')' # see 3.4.1. Addr-spec specification | ||
DTEXT = r'[' + NO_WS_CTL + r'\x21-\x5a\x5e-\x7e]' # see 3.4.1 | ||
DCONTENT = r'(?:' + DTEXT + r'|' + \ | ||
QUOTED_PAIR + r')' # see 3.4.1 | ||
DOMAIN_LITERAL = CFWS + r'?' + r'\[' + \ | ||
r'(?:' + FWS + r'?' + DCONTENT + \ | ||
r')*' + FWS + r'?\]' + CFWS + r'?' # see 3.4.1 | ||
DOMAIN = r'(?:' + DOT_ATOM + r'|' + \ | ||
DOMAIN_LITERAL + r')' # see 3.4.1 | ||
ADDR_SPEC = LOCAL_PART + r'@' + DOMAIN # see 3.4.1 | ||
|
||
# A valid address will match exactly the 3.4.1 addr-spec. | ||
VALID_ADDRESS_REGEXP = '^' + ADDR_SPEC + '$' | ||
|
||
def validate_email(email, mx=False,verify=False): | ||
|
||
"""Indicate whether the given string is a valid email address | ||
according to the 'addr-spec' portion of RFC 2822 (see section | ||
3.4.1). Parts of the spec that are marked obsolete are *not* | ||
included in this test, and certain arcane constructions that | ||
depend on circular definitions in the spec may not pass, but in | ||
general this should correctly identify any email address likely | ||
to be in use as of 2011.""" | ||
try: | ||
assert re.match(VALID_ADDRESS_REGEXP, email) is not None | ||
mx |= verify | ||
if 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: #Server not permits verify user | ||
break | ||
except smtplib.SMTPConnectError: | ||
continue | ||
except (AssertionError, ServerError): | ||
return False | ||
return True | ||
|
||
# import sys | ||
|
||
# sys.modules[__name__],sys.modules['validate_email_module'] = validate_email,sys.modules[__name__] | ||
# from validate_email_module import * |