Python实现获取邮箱内容并解析的方法示例

时间:2021-07-08 03:49:20

本文实例讲述了Python实现获取邮箱内容并解析的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
import poplib
#输入邮件地址, 口令和POP3服务器地址:
email = input('Email: ')
password = input('Password: ')
pop3_server = input('POP3 server: ')
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))
# 连接到POP3服务器:
server = poplib.POP3_SSL(pop3_server, 995)
# 可以打开或关闭调试信息:
server.set_debuglevel(1)
# 可选:打印POP3服务器的欢迎文字:
print(server.getwelcome().decode('utf-8'))
# 身份认证:
server.user(email)
server.pass_(password)
# stat()返回邮件数量和占用空间:
print('Messages: %s. Size: %s' % server.stat())
# list()返回所有邮件的编号:
resp, mails, octets = server.list()
# 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
print(mails)
# 获取最新一封邮件, 注意索引号从1开始:
index = len(mails)
resp, lines, octets = server.retr(index)
# lines存储了邮件的原始文本的每一行,
# 可以获得整个邮件的原始文本:
msg_content = b'\r\n'.join(lines).decode('utf-8')
# 稍后解析出邮件:
msg = Parser().parsestr(msg_content)
print_info(msg)
# 可以根据邮件索引号直接从服务器删除邮件:
# server.dele(index)
# 关闭连接:
server.quit()

希望本文所述对大家Python程序设计有所帮助。

原文链接:https://blog.csdn.net/aNoobCoder/article/details/55103954