1 登录认证

  1. server = 'xxx.xxx'
  2. # 两种方式均可,推荐IMAP4_SSL
  3. mail = imaplib.IMAP4(server) # 默认端口imaplib.IMAP4_PORT 143
  4. mail = imaplib.IMAP4_SSL(server) # 默认端口imaplib.IMAP4_SSL_PORT 993
  5. res = mail.login(username, password)
  6. # 登录成功: ('OK', ['Success login ok'])
  7. # 登录失败:error: command LOGIN illegal in state AUTH, only allowed in states NONAUTH
  8. mail.logout()
  9. # 退出:('BYE', ['LOGOUT received'])

2 查看邮件

  1. # login ...
  2. # 列出目录
  3. print mail.list()[0]
  4. # OK
  5. for each in mail.list()[1]:
  6. print each
  7. # (\HasNoChildren) "/" "INBOX" - 收件箱
  8. # (\HasNoChildren) "/" "Sent Messages" - 已发送邮件
  9. # (\HasNoChildren) "/" "Drafts" - 草稿箱
  10. # (\HasNoChildren) "/" "Deleted Messages" - 已删除邮件
  11. # (\HasNoChildren) "/" "Junk" - 垃圾邮件
  12. # ...
  13. # 选择目录
  14. mail.select('INBOX')
  15. # ('OK', ['11477']) - 11477为该目录下邮件的数目
  16. # 在该目录下按状态条件搜索
  17. mail.search(None, 'UNSEEN')
  18. # ('OK', ['11477'])

3 MailBox状态

Condition Meaning
MESSAGES The number of messages in the mailbox.
RECENT The number of messages with the \\Recent flag set.
UIDNEXT The next unique identifier value of the mailbox.
UIDVALIDITY The unique identifier validity value of the mailbox.
UNSEEN The number of messages which do not have the \\Seen flag set.
  1. # ... select
  2. # 查看未读邮件
  3. mail.search(None, 'unseen') # or new
  4. # 查看已读邮件
  5. mail.search(None, 'seen')
  6. # 按条件搜索 不生效???
  7. mail.search(None, '(SUBJECT "test")')
  8. mail.search(None, '(UNSEEN SUBJECT "test")')
  9. # 修改邮件状态
  10. mail.store('11477', '+FLAGS', '\\seen') # 设为已读
  11. mail.store('11477', '-FLAGS', '\\seen') # 设为未读
  12. # 删除邮件
  13. mail.store('11477', '+FLAGS', '\\Deleted')
  14. mail.store('11477', '-FLAGS', '\\Deleted')
  15. # 永久删除
  16. mail.expunge() # logout时才会真的删除

4 获取邮件内容

  1. # ... select
  2. # 获取header
  3. mail.fetch('11476', '(BODY.PEEK[HEADER])')
  4. # 获取text
  5. mail.fetch('11476', '(BODY.PEEK[TEXT])')
  6. # 获取全部(header+text)
  7. mail.fetch('11476', '(RFC822)')
  8. # 获取flags
  9. mail.fetch('11476', '(FLAGS)')
  10. # ================================
  11. # 解析邮件
  12. import email
  13. typ, data = mail.fetch('11476', '(RFC822)')
  14. msg = email.message_from_string(data[0][1])
  15. print msg['from']
  16. print msg['to']
  17. print msg['subject']
  18. ## subject如果包含中文需要解码
  19. text, enc = email.header.decode_header(msg['subject'])[0]
  20. subject = text.decode(enc) if enc else text
  21. print subject
  22. ## 发信人邮箱获取
  23. ## 邮件正文获取
  24. if msg['sender']:
  25. email_from = msg['sender']
  26. elif re.findall(r'<.+?>', msg['from']):
  27. email_from = re.findall(r'<(.+?)>', msg['from'])[0]
  28. else:
  29. email_from = msg['from']

问题:中文目录解码问题

& 替换成 + ,再 utf-7 解码

  1. for each in mail.list()[1]:
  2. catalog = each.split(' "/" ')[-1].replace('&', '+').decode('utf-7')
  3. print(catalog)
  4. # 选择目录时按原写法即可
  5. mail.select('&UXZO1mWHTvZZOQ-/&VS5UDg-')
  6. # 也可以反向转回去
  7. catalog = '其他文件夹/售后'.decode('utf-8').encode('utf-7').replace('+', '&')
  8. mail.select(catalog)

image.png


参考