电子邮件Ping挂在电子邮件上

时间:2017-01-18 21:02:00

标签: python while-loop try-catch email-validation

我正在尝试使用我发现的脚本来ping电子邮件以确保它们存在。

0

代码工作得很好。但是,在while循环中,我会遇到很多超时错误:

with open(input_list, 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        address = row[0]
        person_name = row[1]+' '+row[2]
        company = row[4]
        match = re.match('^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$', address)
        print("Email for ", person_name)
        print(address)
        if match == None:
            synt = 'Bad Syntax'
            warnings.warn(address + " has bad syntax.")
        else:
            synt = 'Good syntax'
        dom = re.search("@(.*)$", address).group(1)
        print(dom)
        try:
            records = dns.resolver.query(dom, 'MX')
            mxRecord = records[0].exchange
            mxRecord = str(mxRecord)
        except:
            warnings.warn("Issue contacting domain")
            pass
        # Get local server hostname
        host = socket.gethostname()
        # SMTP lib setup (use debug level for full output)
        server = smtplib.SMTP('smtp-mail.outlook.com',587)#will need this for mail sending
        while True:
            try:
                server.set_debuglevel(0)
                # SMTP Conversation
                server.connect(mxRecord)
                server.helo(host)
                server.mail('me@domain.com')
                code, message = server.rcpt(str(address))
                server.quit()
                if code == 250:
                    print('Success')
                    new_row = [address, person_name, company, synt, 'Ping Successful']
                    email_data.append(new_row)
                    with open('cseresult2.csv', 'a+', newline='') as mess:
                        writ = csv.writer(mess, dialect='excel')
                        writ.writerow(email_data[-1])
                else:
                    print('Bad')
                    new_row = [address, person_name, company, synt, 'Ping Bounced']
                    email_data.append(new_row)
                    with open('cseresult2.csv', 'a+', newline='') as mess:
                        writ = csv.writer(mess, dialect='excel')
                        writ.writerow(email_data[-1])
            except:
                continue
            break
        print()
        print('================')
        print()
        time.sleep(3)

while循环已经处理了这个问题,但现在它将挂在电子邮件上而不会遍历列表的其余部分。这是一个项目,所以任何帮助它移动将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果遇到任何类型的持久性错误,您的while循环将设置为永久继续。 try块中有很多代码,因此有很多情况会发生这种情况(例如无法连接到您的电子邮件服务器,无法打开您的csv文件等)。如果没有更多信息,我无法确定这是否正在发生,但解决这个问题肯定不会有害。

由于您唯一关注的错误是TimeoutError,因此您应该明确地捕获该错误,并且仅在这种情况下continue。对于所有其他错误,要么打印错误并打破循环,要么让错误冒出来。您还应该在创建服务器实例时将超时设置为合理的值。最后,如果在一定时间后超时未解决,则for循环结束会更好;即使在持久TimeoutError的情况下,你也不想永远尝试:

### Set a sane timeout
server = smtplib.SMTP('smtp-mail.outlook.com', 587, timeout=1)

### Try a maximum of 10 times
for i in range(10):
    try:
        ### Connect to the email server and write to your file

    except TimeoutError, e:
        print e

    ### All other errors will bubble up

如果你很好奇默认超时是什么,你可以这样做:

import socket

print(socket.getdefaulttimeout())

响应是默认超时,以秒为单位。值None表示您永远不会超时,但对您来说情况似乎并非如此。