# -*- coding: utf-8 -*- import poplib import email from email.parser import Parser from email.header import decode_header from email.utils import parseaddr import time def guess_charset(msg): # 先从msg对象获取编码: charset = msg.get_charset() if charset is None: # 如果获取不到,再从Content-Type字段获取: content_type = msg.get('Content-Type', '').lower() pos = content_type.find('charset=') if pos >= 0: charset = content_type[pos + 8:].strip() return charset #邮件的Subject或者Email中包含的名字都是经过编码后的str,要正常显示,就必须decode: def decode_str(s): value, charset = decode_header(s)[0] if charset: value = value.decode(charset) return value # indent用于缩进显示: RESULT = '' def mail_info(msg,indent=0): global RESULT if indent == 0: # 邮件的From, To, Subject存在于根对象上: for header in ['From', 'To', 'Subject']: value = msg.get(header, '') if value: if header=='Subject': # 需要解码Subject字符串: value = decode_str(value) else: # 需要解码Email地址: hdr, addr = parseaddr(value) name = decode_str(hdr) value = u'%s <%s>' % (name, addr) # print('%s%s: %s' % (' ' * indent, header, value)) RESULT += ('%s%s: %s' % (' ' * indent, header, value)) RESULT += '\r\n' if (msg.is_multipart()): # 如果邮件对象是一个MIMEMultipart, # get_payload()返回list,包含所有的子对象: parts = msg.get_payload() for n, part in enumerate(parts): # print('%spart %s' % (' ' * indent, n)) # print('%s--------------------' % (' ' * indent)) # 递归打印每一个子对象: mail_info(part,indent + 1) else: # 邮件对象不是一个MIMEMultipart, # 就根据content_type判断: content_type = msg.get_content_type() if content_type=='text/plain' or content_type=='text/html': # 纯文本或HTML内容: content = msg.get_payload(decode=True) # 要检测文本编码: charset = guess_charset(msg) if charset: try: content = content.decode(charset) except: content = content.decode('utf-8', errors='ignore').encode('utf-8') # print('%sText: %s' % ('' * indent, content + '...')) RESULT += ('%sText: %s' % ('' * indent, content)) else: # 不是文本,作为附件处理: # print('%sAttachment: %s' % (' ' * indent, content_type)) RESULT += ('%sAttachment: %s' % (' ' * indent, content_type)) return RESULT def judge_mail(content,condition): res = True for l in range(0,len(condition)): if condition[l] not in content: res = False break return res #接收邮件:邮箱、密码、域名、邮件数量 def receive_mail(email,password,pop3_server,num): res = '' server = poplib.POP3(pop3_server) #server.set_debuglevel(1) # print(server.getwelcome()) # 认证: server.user(email) server.pass_(password) # print('Messages: %s. Size: %s' % server.stat()) resp, mails, octets = server.list() for i in range(0,num): # 获取最新一封邮件, 注意索引号从1开始: resp, lines, octets = server.retr(len(mails) - i) # 解析邮件: msg_content = b'\r\n'.join(lines).decode('utf-8') msg = Parser().parsestr(msg_content) res += mail_info(msg) # 关闭连接: # server.quit() return res #删除邮件 def delete_mail(email,password,pop3_server): server = poplib.POP3(pop3_server) #server.set_debuglevel(1) # print(server.getwelcome()) # 认证: server.user(email) server.pass_(password) # print('Messages: %s. Size: %s' % server.stat()) resp, mails, octets = server.list() # 慎重:将直接从服务器删除邮件: server.dele(len(mails)) # 关闭连接: server.quit() return True if __name__ == '__main__': email = 'jiangyf@wondershare.cn' password = ' pop3_server = 'imap.wondershare.cn' print(receive_mail(email,password,pop3_server,2)) # condition = ['Thank you for shopping with us www.wondershare.com1.','2.PID'] # #循环检查三次邮件 # for i in range(3): # result = receive_mail(email,password,pop3_server,2) # res = judge_mail(result,condition) # if res == True: # print(res) # break # else: # print('第' + str(i) + '次轮循') # time.sleep(30) # print(delete_mail(email,password,pop3_server))