前面我们讲到的接收邮件中,接收到的邮件把它打印出来,会发现是一堆很复杂的东西,很难阅读。这就需要我们去解析它
这里以pop3作为接收邮件的协议,并对邮件进行解析。
代码语言:javascript复制import poplib
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
user = 'xxx@xx.xx'
password = 'xxxxx'
pop_server = poplib.POP3_SSL('pop.xxx.xxx', 995)
print(pop_server.getwelcome().decode('utf-8'))
pop_server.user(user)
pop_server.pass_(password)
print('Server stat', pop_server.stat())
#获取所有邮件列表
resp, mails, octets = pop_server.list()
print(mails)
#获取最新的一封邮件(序号最大的)。邮件索引从1开始计数
index= len(mails)
resp, lines, octets = pop_server.retr(index)
msg_content = b'rn'.join(lines).decode('utf-8')
#解析出邮件
msg = Parser().parsestr(msg_content)
pop_server.quit()
def decode_email(s):
if not s:
return ''
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
#打印邮件的发件人、收件人和主题
mail_from = msg.get('From', "")
hdr, mail_from_addr = parseaddr(mail_from)
mail_from_name = decode_email(hdr)
print('发件人', mail_from_name, mail_from_addr)
mail_to = msg.get('To', "")
hdr, mail_to_addr = parseaddr(mail_to)
mail_to_name = decode_email(hdr)
print('收件人', mail_to_name, mail_to_addr)
subject = decode_email(msg.get('Subject', ''))
print('主题:', subject)
#递归解析邮件
def decode_mime(msg):
if msg.is_multipart():
parts = msg.get_payload()
for part in parts:
print(decode_mime(part))
else:
content_type = msg.get_content_type()
if content_type in ('text/plain', 'text/html'):
content = msg.get_payload(decode=True)
print(content.decode('utf-8'))
else:
print('Attachment', content_type)
decode_mime(msg)