|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from email.parser import Parser |
| 5 | +from email.header import decode_header |
| 6 | +from email.utils import parseaddr |
| 7 | + |
| 8 | +import poplib |
| 9 | + |
| 10 | +# 输入邮件地址, 口令和POP3服务器地址: |
| 11 | +email = input('Email: ') |
| 12 | +password = input('Password: ') |
| 13 | +pop3_server = input('POP3 server: ') |
| 14 | + |
| 15 | +def guess_charset(msg): |
| 16 | + charset = msg.get_charset() |
| 17 | + if charset is None: |
| 18 | + content_type = msg.get('Content-Type', '').lower() |
| 19 | + pos = content_type.find('charset=') |
| 20 | + if pos >= 0: |
| 21 | + charset = content_type[pos + 8:].strip() |
| 22 | + return charset |
| 23 | + |
| 24 | +def decode_str(s): |
| 25 | + value, charset = decode_header(s)[0] |
| 26 | + if charset: |
| 27 | + value = value.decode(charset) |
| 28 | + return value |
| 29 | + |
| 30 | +def print_info(msg, indent=0): |
| 31 | + if indent == 0: |
| 32 | + for header in ['From', 'To', 'Subject']: |
| 33 | + value = msg.get(header, '') |
| 34 | + if value: |
| 35 | + if header=='Subject': |
| 36 | + value = decode_str(value) |
| 37 | + else: |
| 38 | + hdr, addr = parseaddr(value) |
| 39 | + name = decode_str(hdr) |
| 40 | + value = u'%s <%s>' % (name, addr) |
| 41 | + print('%s%s: %s' % (' ' * indent, header, value)) |
| 42 | + if (msg.is_multipart()): |
| 43 | + parts = msg.get_payload() |
| 44 | + for n, part in enumerate(parts): |
| 45 | + print('%spart %s' % (' ' * indent, n)) |
| 46 | + print('%s--------------------' % (' ' * indent)) |
| 47 | + print_info(part, indent + 1) |
| 48 | + else: |
| 49 | + content_type = msg.get_content_type() |
| 50 | + if content_type=='text/plain' or content_type=='text/html': |
| 51 | + content = msg.get_payload(decode=True) |
| 52 | + charset = guess_charset(msg) |
| 53 | + if charset: |
| 54 | + content = content.decode(charset) |
| 55 | + print('%sText: %s' % (' ' * indent, content + '...')) |
| 56 | + else: |
| 57 | + print('%sAttachment: %s' % (' ' * indent, content_type)) |
| 58 | + |
| 59 | +# 连接到POP3服务器: |
| 60 | +server = poplib.POP3(pop3_server) |
| 61 | +# 可以打开或关闭调试信息: |
| 62 | +server.set_debuglevel(1) |
| 63 | +# 可选:打印POP3服务器的欢迎文字: |
| 64 | +print(server.getwelcome().decode('utf-8')) |
| 65 | +# 身份认证: |
| 66 | +server.user(email) |
| 67 | +server.pass_(password) |
| 68 | +# stat()返回邮件数量和占用空间: |
| 69 | +print('Messages: %s. Size: %s' % server.stat()) |
| 70 | +# list()返回所有邮件的编号: |
| 71 | +resp, mails, octets = server.list() |
| 72 | +# 可以查看返回的列表类似[b'1 82923', b'2 2184', ...] |
| 73 | +print(mails) |
| 74 | +# 获取最新一封邮件, 注意索引号从1开始: |
| 75 | +index = len(mails) |
| 76 | +resp, lines, octets = server.retr(index) |
| 77 | +# lines存储了邮件的原始文本的每一行, |
| 78 | +# 可以获得整个邮件的原始文本: |
| 79 | +msg_content = b'\r\n'.join(lines).decode('utf-8') |
| 80 | +# 稍后解析出邮件: |
| 81 | +msg = Parser().parsestr(msg_content) |
| 82 | +print_info(msg) |
| 83 | +# 可以根据邮件索引号直接从服务器删除邮件: |
| 84 | +# server.dele(index) |
| 85 | +# 关闭连接: |
| 86 | +server.quit() |
0 commit comments