我正在尝试使用python验证一些电子邮件,除了那些与我自己的电子邮件服务器匹配的电子邮件之外,我一直在获取所有电子邮件
import re
import smtplib
import dns.resolver
import socket
import smtplib
# Address used for SMTP MAIL FROM command
fromAddress = 'fromMyEmail@mydomain.com'#
input = open("input.txt", "r")
output = open("output.txt", "w+")
# Simple Regex for syntax checking
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
_VALID = 'VALID'
_IN_VALID = 'IN_VALID'
output.write('{}\t{}\t{}\t{}\t{}\n'.format('email', 'domain', 'code', 'message', 'is_valid'))
for inputAddress in input.readlines():
print('parsing ', inputAddress)
try:
code = 0
message = None
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
addressToVerify = str(inputAddress)
# Syntax check
match = re.match(regex, addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
# Get domain for DNS lookup
splitAddress = addressToVerify.split('@')
domain = str(splitAddress[1])
print('Domain:', domain)
# MX record lookup
records = dns.resolver.query(domain, 'MX')
print('exchanging')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
print('mxRecord ', mxRecord)
# SMTP Conversation
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
output.write(
'{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain.strip(), code, str(message).strip(),
_VALID))
else:
output.write(
'{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain.strip(), code, str(message).strip(),
_IN_VALID))
except Exception as e:
output.write('{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain, -1, str(e).strip(), _IN_VALID))
output.close()
但我一直得到错误[Errno 111] Connection refused
目前没有回答
相关问题 更多 >
编程相关推荐