# -*- coding: utf-8 -*- import poplib import email from email.parser import Parser from email.header import decode_header from email.utils import parseaddr ‘‘‘ 用Python的poplib模块收取邮件分两步:第一步是用POP3协议把邮件获取到本地, 第二步是用email模块把原始邮件解析为Message对象,然后,用适当的形式把邮件内容展示给用户即可。 ‘‘‘ #猜测编码格式 def guess_charset(msg): charset = msg.get_charset() if charset is None: content_type = msg.get(‘Content-Type‘, ‘‘).lower() pos = content_type.find(‘charset=‘) if pos >= 0: charset = content_type[pos + 8:].strip() return charset #解码 def decode_str(s): value, charset = decode_header(s)[0] if charset: value = value.decode(charset) return value #解析邮件内容 def print_info(msg, indent=0): if indent == 0: for header in [‘From‘, ‘To‘, ‘Subject‘]: value = msg.get(header, ‘‘) if value: if header==‘Subject‘: value = decode_str(value) else: hdr, addr = parseaddr(value) name = decode_str(hdr) value = u‘%s <%s>‘ % (name, addr) print(‘%s%s: %s‘ % (‘ ‘ * indent, header, value)) if (msg.is_multipart()): parts = msg.get_payload() for n, part in enumerate(parts): print(‘%spart %s‘ % (‘ ‘ * indent, n)) print(‘%s--------------------‘ % (‘ ‘ * indent)) print_info(part, indent + 1) else: content_type = msg.get_content_type() if content_type==‘text/plain‘ or content_type==‘text/html‘: content = msg.get_payload(decode=True) charset = guess_charset(msg) if charset: content = content.decode(charset) print(‘%sText: %s‘ % (‘ ‘ * indent, content + ‘...‘)) else: print(‘%sAttachment: %s‘ % (‘ ‘ * indent, content_type)) email = raw_input(‘Email: ‘) password = raw_input(‘Password: ‘) pop3_server = raw_input(‘POP3 server: ‘) #加密 #server = poplib.POP3_SSL(pop3_server, port = 995) 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() # 获取最新一封邮件, 注意索引号从1开始: resp, lines, octets = server.retr(len(mails)) # 解析邮件: msg = Parser().parsestr(‘\r\n‘.join(lines)) # 打印邮件内容: print_info(msg) # 慎重:将直接从服务器删除邮件: # server.dele(len(mails)) # 关闭连接: server.quit()
借鉴 https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001408244819215430d726128bf4fa78afe2890bec57736000。