├── verify_email ├── tests │ ├── __init__.py │ └── test_verify_email.py ├── __init__.py ├── fast_verify.py └── verify_email.py ├── .gitignore ├── verify_email.egg-info ├── dependency_links.txt ├── top_level.txt ├── requires.txt ├── SOURCES.txt └── PKG-INFO ├── dist ├── verify_email-2.0.0.tar.gz ├── verify_email-2.3.1.tar.gz ├── verify_email-2.4.0.tar.gz ├── verify_email-2.4.1.tar.gz ├── verify_email-2.4.2.tar.gz ├── verify_email-2.4.3.tar.gz ├── verify_email-2.4.0-py3.7.egg ├── verify_email-2.4.1-py3.9.egg ├── verify_email-2.4.3.dev0.tar.gz ├── verify_email-2.4.4.dev0.tar.gz ├── verify_email-2.0.0-py2-none-any.whl ├── verify_email-2.2.0-py2-none-any.whl ├── verify_email-2.3.1-py3-none-any.whl ├── verify_email-2.4.0-py3-none-any.whl ├── verify_email-2.4.1-py3-none-any.whl ├── verify_email-2.4.2-py3-none-any.whl ├── verify_email-2.4.3-py3-none-any.whl ├── verify_email-2.4.3.dev0-py3.10.egg ├── verify_email-2.1.0.dev0-py2-none-any.whl ├── verify_email-2.2.0.dev0-py2-none-any.whl ├── verify_email-2.3.0.dev0-py2-none-any.whl ├── verify_email-2.4.0-py2.py3-none-any.whl ├── verify_email-2.4.3.dev0-py3-none-any.whl ├── verify_email-2.4.4.dev0-py3-none-any.whl ├── verify_email-2.2.0.macosx-10.13-x86_64.tar.gz ├── verify_email-2.1.0.dev0.macosx-10.13-x86_64.tar.gz ├── verify_email-2.2.0.dev0.macosx-10.13-x86_64.tar.gz └── verify_email-2.3.0.dev0.macosx-10.13-x86_64.tar.gz ├── setup.py ├── LICENSE ├── examples.py └── README.md /verify_email/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .vscode 3 | build/ -------------------------------------------------------------------------------- /verify_email.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /verify_email.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | verify_email 2 | -------------------------------------------------------------------------------- /verify_email.egg-info/requires.txt: -------------------------------------------------------------------------------- 1 | aiosmtpd 2 | aiodns 3 | -------------------------------------------------------------------------------- /verify_email/__init__.py: -------------------------------------------------------------------------------- 1 | from .verify_email import * 2 | -------------------------------------------------------------------------------- /dist/verify_email-2.0.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.0.0.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.3.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.3.1.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.0.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.1.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.2.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.2.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.3.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.3.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.0-py3.7.egg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.0-py3.7.egg -------------------------------------------------------------------------------- /dist/verify_email-2.4.1-py3.9.egg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.1-py3.9.egg -------------------------------------------------------------------------------- /dist/verify_email-2.4.3.dev0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.3.dev0.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.4.4.dev0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.4.dev0.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.0.0-py2-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.0.0-py2-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.2.0-py2-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.2.0-py2-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.3.1-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.3.1-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.0-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.0-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.1-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.1-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.2-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.2-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.3-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.3-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.3.dev0-py3.10.egg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.3.dev0-py3.10.egg -------------------------------------------------------------------------------- /dist/verify_email-2.1.0.dev0-py2-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.1.0.dev0-py2-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.2.0.dev0-py2-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.2.0.dev0-py2-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.3.0.dev0-py2-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.3.0.dev0-py2-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.0-py2.py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.0-py2.py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.3.dev0-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.3.dev0-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.4.4.dev0-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.4.4.dev0-py3-none-any.whl -------------------------------------------------------------------------------- /dist/verify_email-2.2.0.macosx-10.13-x86_64.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.2.0.macosx-10.13-x86_64.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.1.0.dev0.macosx-10.13-x86_64.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.1.0.dev0.macosx-10.13-x86_64.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.2.0.dev0.macosx-10.13-x86_64.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.2.0.dev0.macosx-10.13-x86_64.tar.gz -------------------------------------------------------------------------------- /dist/verify_email-2.3.0.dev0.macosx-10.13-x86_64.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kakshay21/verify_email/HEAD/dist/verify_email-2.3.0.dev0.macosx-10.13-x86_64.tar.gz -------------------------------------------------------------------------------- /verify_email.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | LICENSE 2 | README.md 3 | setup.py 4 | verify_email/__init__.py 5 | verify_email/fast_verify.py 6 | verify_email/verify_email.py 7 | verify_email.egg-info/PKG-INFO 8 | verify_email.egg-info/SOURCES.txt 9 | verify_email.egg-info/dependency_links.txt 10 | verify_email.egg-info/requires.txt 11 | verify_email.egg-info/top_level.txt 12 | verify_email/tests/__init__.py 13 | verify_email/tests/test_verify_email.py -------------------------------------------------------------------------------- /verify_email/fast_verify.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from verify_email import verify_email 3 | 4 | import multiprocessing 5 | 6 | 7 | emails = ["k.akshay9721@gmail.com", "xyz231312dasdaf@gmail.com", "foo@bar.com", "ex@example.com"] # add emails 8 | b = datetime.now() 9 | 10 | 11 | def validate(email): 12 | a = datetime.now() 13 | value = verify_email(email) 14 | delta = datetime.now() - a 15 | print(value, email, (delta.microseconds + delta.microseconds/1E6)) 16 | 17 | 18 | pool = multiprocessing.Pool() 19 | result = pool.map(validate, emails) 20 | delta = datetime.now() - b 21 | print(delta.total_seconds()) 22 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open('README.md', 'r') as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name='verify_email', 8 | version='2.4.4-dev', 9 | author='Kumar Akshay', 10 | author_email='k.akshay9721@gmail.com', 11 | description='A small package for email verification', 12 | long_description=long_description, 13 | long_description_content_type='text/markdown', 14 | url='https://github.com/kakshay21/verify_email', 15 | packages=setuptools.find_packages(), 16 | classifiers=( 17 | 'Programming Language :: Python :: 3.7', 18 | 'License :: OSI Approved :: MIT License', 19 | 'Operating System :: OS Independent', 20 | ), 21 | install_requires=[ 22 | # -*- Extra requirements: -*- 23 | 'aiosmtpd', 24 | 'aiodns' 25 | ], 26 | ) -------------------------------------------------------------------------------- /verify_email/tests/test_verify_email.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from verify_email import verify_email 4 | 5 | 6 | class TestVerifyEmail(unittest.TestCase): 7 | def setUp(self): 8 | self.emails = [ 9 | 'k.akshay9721@gmail.com', 10 | 'some.email.address.that.does.not.exist@gmail.com', 11 | 'foo@bar.com', 12 | 'ex@example.com' 13 | ] 14 | 15 | def test_valid_email(self): 16 | self.assertEqual(True, verify_email(self.emails[0])) 17 | 18 | def test_invalid_email(self): 19 | self.assertEqual(False, verify_email(self.emails[1])) 20 | 21 | def test_email_with_debugger(self): 22 | self.assertEqual(False, verify_email(self.emails[2], debug=True)) 23 | 24 | def test_multiple_emails(self): 25 | self.assertEqual([True, False, False, False], verify_email(self.emails)) 26 | 27 | 28 | if __name__ == '__main__': 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 The Python Packaging Authority 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /examples.py: -------------------------------------------------------------------------------- 1 | from verify_email import verify_email, verify_email_async 2 | import time 3 | import asyncio 4 | 5 | 6 | emails = [ 7 | 'k.akshay9721@gmail.com', 8 | 'some.email.address.that.does.not.exist@gmail.com', 9 | 'foo@bar.com', 10 | 'ex@example.com' 11 | ] 12 | 13 | def time_it(func, *args, **kwargs): 14 | start = time.time() 15 | func(*args, **kwargs) 16 | print('used:', time.time() - start, 'seconds') 17 | 18 | 19 | def single(email): 20 | print(email, 'is', verify_email(email) and 'valid' or 'invalid') 21 | 22 | 23 | def multiple(): 24 | print('\t'.join(emails)) 25 | print('\t'.join(i and 'valid' or 'invalid' for i in verify_email(emails))) 26 | 27 | async def main(): 28 | print('\t'.join(emails)) 29 | print('\t'.join(i and 'valid' or 'invalid' for i in await verify_email_async(emails))) 30 | 31 | 32 | print('### single') 33 | for email in emails: 34 | time_it(single, email) 35 | print('### multiple') 36 | time_it(multiple) 37 | print('note that network condition may have impact on test results') 38 | print("asyncio test") 39 | asyncio.new_event_loop().run_until_complete(main()) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # verify-email 2 | 3 | `verify-email` can verify any email address by efficiently checking the domain name and pinging the handler to verify its existence. 4 | 5 | ## Features 6 | 7 | - Syntax checks 8 | - MX(Mail Exchange records) verification 9 | - Email Handler verification 10 | - Caching domain lookups to improve performance 11 | - Supports `asyncio` for concurrency 12 | - For `multiprocessing` usage, see [fast_verify.py](https://github.com/kakshay21/verify_email/blob/master/verify_email/fast_verify.py)). 13 | 14 | ## Compatibility 15 | - Written in Python 3.7. 16 | - Supports Python 3.7+. 17 | - It should work on Linux, Mac and Windows. 18 | 19 | ## Installation 20 | ### From [pypi.org](https://pypi.org/project/verify-email/) 21 | ``` 22 | $ pip install verify-email 23 | ``` 24 | ### From source code 25 | ``` 26 | $ git clone https://github.com/kakshay21/verify_email 27 | $ cd verify_email 28 | $ virtualenv env 29 | $ source env/bin/activate 30 | $ python setup.py develop 31 | ``` 32 | 33 | ## Usage 34 | ``` 35 | >>> from verify_email import verify_email 36 | >>> verify_email('foo@bar.com') 37 | False 38 | >>> verify_email(['foo@bar.com', 'example@foo.com']) 39 | [False, False] 40 | ``` 41 | Also, note that some emails will likely fail in validation, if so you can check the reason of failure 42 | using debug flag. 43 | ``` 44 | >>> from verify_email import verify_email 45 | >>> verify_email('foo@bar.com', debug=True) 46 | ``` 47 | 48 | see for more examples [examples.py](https://github.com/kakshay21/verify_email/blob/master/examples.py) 49 | 50 | ## Contribute 51 | - Issue Tracker: https://github.com/kakshay21/verify_email/issues 52 | - Source Code: https://github.com/kakshay21/verify_email 53 | 54 | ## Support 55 | If you are having issues, please create an issue for it. And feel free to contribute as well 😄. 56 | -------------------------------------------------------------------------------- /verify_email.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.1 2 | Name: verify-email 3 | Version: 2.4.4.dev0 4 | Summary: A small package for email verification 5 | Home-page: https://github.com/kakshay21/verify_email 6 | Author: Kumar Akshay 7 | Author-email: k.akshay9721@gmail.com 8 | Classifier: Programming Language :: Python :: 3.7 9 | Classifier: License :: OSI Approved :: MIT License 10 | Classifier: Operating System :: OS Independent 11 | Description-Content-Type: text/markdown 12 | License-File: LICENSE 13 | 14 | # verify-email 15 | 16 | `verify-email` can verify any email address by efficiently checking the domain name and pinging the handler to verify its existence. 17 | 18 | ## Features 19 | 20 | - Syntax checks 21 | - MX(Mail Exchange records) verification 22 | - Email Handler verification 23 | - Caching domain lookups to improve performance 24 | - Supports `asyncio` for concurrency 25 | - For `multiprocessing` usage, see [fast_verify.py](https://github.com/kakshay21/verify_email/blob/master/verify_email/fast_verify.py)). 26 | 27 | ## Compatibility 28 | - Written in Python 3.7. 29 | - Supports Python 3.7+. 30 | - It should work on Linux, Mac and Windows. 31 | 32 | ## Installation 33 | ### From [pypi.org](https://pypi.org/project/verify-email/) 34 | ``` 35 | $ pip install verify-email 36 | ``` 37 | ### From source code 38 | ``` 39 | $ git clone https://github.com/kakshay21/verify_email 40 | $ cd verify_email 41 | $ virtualenv env 42 | $ source env/bin/activate 43 | $ python setup.py develop 44 | ``` 45 | 46 | ## Usage 47 | ``` 48 | >>> from verify_email import verify_email 49 | >>> verify_email('foo@bar.com') 50 | False 51 | >>> verify_email(['foo@bar.com', 'example@foo.com']) 52 | [False, False] 53 | ``` 54 | Also, note that some emails will likely fail in validation, if so you can check the reason of failure 55 | using debug flag. 56 | ``` 57 | >>> from verify_email import verify_email 58 | >>> verify_email('foo@bar.com', debug=True) 59 | ``` 60 | 61 | see for more examples [examples.py](https://github.com/kakshay21/verify_email/blob/master/examples.py) 62 | 63 | ## Contribute 64 | - Issue Tracker: https://github.com/kakshay21/verify_email/issues 65 | - Source Code: https://github.com/kakshay21/verify_email 66 | 67 | ## Support 68 | If you are having issues, please create an issue for it. And feel free to contribute as well 😄. 69 | -------------------------------------------------------------------------------- /verify_email/verify_email.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import aiodns 3 | import logging 4 | import re 5 | import smtplib 6 | import socket 7 | import threading 8 | import collections.abc as abc 9 | import sys 10 | 11 | EMAIL_REGEX = r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)' 12 | MX_DNS_CACHE = {} 13 | MX_CHECK_CACHE = {} 14 | 15 | # Set up logging on module load and avoid adding 'ch' or 'logger' to module 16 | # namespace. We could assign the logger to a module level name, but it is only 17 | # used by two functions, and this approach demonstrates using the 'logging' 18 | # namespace to retrieve arbitrary loggers. 19 | 20 | def setup_module_logger(name): 21 | """Set up module level logging with formatting""" 22 | logger = logging.getLogger(name) 23 | ch = logging.StreamHandler() 24 | # Really should not be configuring formats in a library, see 25 | # https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library 26 | formatter = logging.Formatter( 27 | '%(asctime)s - %(name)s - %(levelname)s - %(message)s' 28 | ) 29 | ch.setFormatter(formatter) 30 | logger.addHandler(ch) 31 | 32 | 33 | setup_module_logger('verify_email') 34 | 35 | 36 | def is_list(obj): 37 | return isinstance(obj, abc.Sequence) and not isinstance(obj, str) 38 | 39 | async def get_mx_ip(hostname): 40 | '''Get MX record by hostname. 41 | ''' 42 | if hostname not in MX_DNS_CACHE: 43 | try: 44 | resolver = aiodns.DNSResolver() 45 | MX_DNS_CACHE[hostname] = await resolver.query(hostname, 'MX') 46 | except aiodns.error.DNSError as e: 47 | MX_DNS_CACHE[hostname] = None 48 | return MX_DNS_CACHE[hostname] 49 | 50 | 51 | async def get_mx_hosts(email): 52 | '''Caching the result in MX_DNS_CACHE to improve performance. 53 | ''' 54 | hostname = email[email.find('@') + 1:] 55 | if hostname in MX_DNS_CACHE: 56 | mx_hosts = MX_DNS_CACHE[hostname] 57 | else: 58 | mx_hosts = await get_mx_ip(hostname) 59 | return mx_hosts 60 | 61 | 62 | 63 | async def handler_verify(mx_hosts, email, timeout=None): 64 | for mx in mx_hosts: 65 | res = await network_calls(mx, email, timeout) 66 | if res: 67 | return res 68 | return False 69 | 70 | 71 | async def syntax_check(email): 72 | if re.match(EMAIL_REGEX, email): 73 | return True 74 | return False 75 | 76 | 77 | async def _verify_email(email, timeout=None, verify=True): 78 | '''Validate email by syntax check, domain check and handler check. 79 | ''' 80 | is_valid_syntax = await syntax_check(email) 81 | if is_valid_syntax: 82 | if verify: 83 | mx_hosts = await get_mx_hosts(email) 84 | if mx_hosts is None: 85 | return False 86 | else: 87 | return await handler_verify(mx_hosts, email, timeout) 88 | else: 89 | return False 90 | 91 | def verify_email(emails, timeout=None, verify=True, debug=False): 92 | if debug: 93 | logger = logging.getLogger('verify_email') 94 | logger.setLevel(logging.DEBUG) 95 | result = [] 96 | if not is_list(emails): 97 | emails = [emails] 98 | 99 | loop = asyncio.new_event_loop() 100 | 101 | for email in emails: 102 | resp = loop.run_until_complete(_verify_email(email, timeout, verify)) 103 | result.append(resp) 104 | 105 | return result if len(result) > 1 else result[0] 106 | 107 | async def verify_email_async(emails, timeout=None, verify=True, debug=False): 108 | if debug: 109 | logger = logging.getLogger('verify_email') 110 | logger.setLevel(logging.DEBUG) 111 | result = [] 112 | if not is_list(emails): 113 | emails = [emails] 114 | 115 | 116 | for email in emails: 117 | result.append(await _verify_email(email, timeout, verify)) 118 | 119 | return result if len(result) > 1 else result[0] 120 | 121 | async def network_calls(mx, email, timeout=20): 122 | logger = logging.getLogger('verify_email') 123 | result = False 124 | try: 125 | smtp = smtplib.SMTP(mx.host, timeout=timeout) 126 | status, _ = smtp.ehlo() 127 | if status >= 400: 128 | smtp.quit() 129 | logger.debug(f'{mx} answer: {status} - {_}\n') 130 | return False 131 | smtp.mail('') 132 | status, _ = smtp.rcpt(email) 133 | if status >= 400: 134 | logger.debug(f'{mx} answer: {status} - {_}\n') 135 | result = False 136 | if status >= 200 and status <= 250: 137 | result = True 138 | 139 | logger.debug(f'{mx} answer: {status} - {_}\n') 140 | smtp.quit() 141 | 142 | except smtplib.SMTPServerDisconnected: 143 | logger.debug(f'Server does not permit verify user, {mx} disconnected.\n') 144 | except smtplib.SMTPConnectError: 145 | logger.debug(f'Unable to connect to {mx}.\n') 146 | except socket.timeout as e: 147 | logger.debug(f'Timeout connecting to server {mx}: {e}.\n') 148 | return None 149 | except socket.error as e: 150 | logger.debug(f'ServerError or socket.error exception raised {e}.\n') 151 | return None 152 | 153 | return result 154 | --------------------------------------------------------------------------------