forked from Tzeross/verifyemail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyemail.py
79 lines (64 loc) · 2.02 KB
/
verifyemail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''
在线验证邮箱真实性
'''
import random
import smtplib
import logging
import time
import dns.resolver
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s [line:%(lineno)d] - %(levelname)s: %(message)s')
logger = logging.getLogger()
def fetch_mx(host):
'''
解析服务邮箱
:param host:
:return:
'''
logger.info('正在查找邮箱服务器')
answers = dns.resolver.query(host, 'MX')
res = [str(rdata.exchange)[:-1] for rdata in answers]
logger.info('查找结果为:%s' % res)
return res
def verify_istrue(email):
'''
:param email:
:return:
'''
email_list = []
email_obj = {}
final_res = {}
if isinstance(email, str) or isinstance(email, bytes):
email_list.append(email)
else:
email_list = email
for em in email_list:
name, host = em.split('@')
if email_obj.get(host):
email_obj[host].append(em)
else:
email_obj[host] = [em]
for key in email_obj.keys():
host = random.choice(fetch_mx(key))
logger.info('正在连接服务器...:%s' % host)
s = smtplib.SMTP(host, timeout=10)
for need_verify in email_obj[key]:
helo = s.docmd('HELO chacuo.net')
logger.debug(helo)
send_from = s.docmd('MAIL FROM:<[email protected]>')
logger.debug(send_from)
send_from = s.docmd('RCPT TO:<%s>' % need_verify)
logger.debug(send_from)
if send_from[0] == 250 or send_from[0] == 451:
final_res[need_verify] = True # 存在
elif send_from[0] == 550:
final_res[need_verify] = False # 不存在
else:
final_res[need_verify] = None # 未知
s.close()
return final_res
if __name__ == '__main__':
final_list = verify_istrue(['[email protected]',
])
print(final_list)