使用python中的imap获取带有正文的所有邮件 [英] Get all the mail with body using imap in python

查看:495
本文介绍了使用python中的imap获取带有正文的所有邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,想在gmail帐户中获取包含正文的所有邮件,但是我只能获取最早的电子邮件. 我的代码如下,谢谢您的帮助.

I am new to python and want to get all the mails with the body in the gmail account , but i am able to get only the oldest email. My code is given below , thanks for help in advance.

#!/usr/bin/env python


import getpass
import imaplib
import email
from email.parser import HeaderParser

M = imaplib.IMAP4_SSL("imap.gmail.com", 993)
add = raw_input("Email address: ")
password = getpass.getpass()
M.login(add, password)

M.select()
resp, data = M.FETCH(1, '(RFC822)')
mail = email.message_from_string(data[0][1])

for part in mail.walk():
  # multipart are just containers, so we skip them
  if part.get_content_maintype() == 'multipart':
      continue

  # we are interested only in the simple text messages
  if part.get_content_subtype() != 'plain':
    continue

  payload = part.get_payload()
  print payload

推荐答案

您可以尝试高级库- https: //github.com/ikvk/imap_tools

# get list of email body from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'password') as mailbox:
    body_set = [msg.body for msg in mailbox.fetch()]

这篇关于使用python中的imap获取带有正文的所有邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆