如何使用python imap打印多个电子邮件正文?

时间:2014-02-28 10:32:23

标签: email python-2.7 imap

我非常擅长编码,这是我第一次真正尝试做这样的事情。 我想打印出多个至少两周的电子邮件正文。 (稍后在我完成的程序中,我想让这个可更改)

我看过其他类似的帖子,但我无法让它发挥作用。 我发布的代码是我目前已经走了多远。我可以打印出最新的电子邮件,但我该如何继续呢?

import imaplib
import sys


mail = imaplib.IMAP4_SSL('Server')
mail.login('User', 'Password')

mail.select('Inbox')
result, data = mail.uid('search', None, 'ALL')
ids = data[0]
id_list = ids.split()
latest_email_uid = int(id_list[-1])

for i in range(latest_email_uid, latest_email_uid-5, -1):
    result, data = mail.uid('fetch', i, '(RFC822)')
raw_email = data[0][1]

import email

email_message = email.message_from_string(raw_email)

print email_message.get_payload(decode = True)

def get_first_text_block(self, email_message_instance):
    maintype = email_message_instance.get_content_maintype()
    if maintype == 'multipart':
       for part in email_message_instance.get_payload():
        if part.get_content_maintype() == 'text':
            return part.get_payload
elif maintype == "text":
    return email_message_instance.get_payload()

谢谢!

更新 新代码!

import imaplib
import sys
import email

mail = imaplib.IMAP4_SSL('server')
mail.login('user', 'password')


mail.select('Inbox')
result, data = mail.uid('search', None, 'ALL')

ids = data[0]
id_list = ids.split()
latest_email_uid = int(id_list[-1])

raw_emails = []
for i in range(latest_email_uid, latest_email_uid-5, -1):
   result, data = mail.uid('fetch', i, '(RFC822)')
   raw_emails.append(data[0][1])

for raw_email in raw_emails:
        email_message = email.message_from_string(raw_email)


print email_message.get_payload(decode = True)

def get_first_text_block(self, email_message_instance):
    maintype = email_message_instance.get_content_maintype()
    if maintype == 'multipart':
        for part in email_message_instance.get_payload():
            if part.get_content_maintype() == 'text':
                return part.get_payload
    elif maintype == "text":
        return email_message_instance.get_payload()

这就是它的外观,我可能犯了一些错误,导致只打印一个电子邮件正文。非常感谢帮助!

2 个答案:

答案 0 :(得分:1)

我认为你的问题在这里:

for i in range(latest_email_uid, latest_email_uid-5, -1):
    result, data = mail.uid('fetch', i, '(RFC822)')
raw_email = data[0][1]

循环后,data仅包含您迭代的最后一封电子邮件。相反,请获取电子邮件列表:

raw_emails = []
for i in range(latest_email_uid, latest_email_uid-5, -1):
    result, data = mail.uid('fetch', i, '(RFC822)')
    raw_emails.append(data[0][1])

您现在可以迭代这些:

for raw_email in raw_emails:
    email_message = email.message_from_string(raw_email)
    ...

(注意:您应该关注PEP-0008并将所有import语句放在顶部 - 这样可以更轻松地阅读和理解代码,特别是在代码增长时。)


修改

您的修订版仅打印一次,因为在print循环运行后for 运行,而不是每次迭代。缩进非常重要:

for raw_email in raw_emails:
    email_message = email.message_from_string(raw_email)
    print email_message.get_payload(decode=True) # note indentation

答案 1 :(得分:0)

@jonrsharpe已经指出代码中的缩进错误。

这是Python 3脚本,你可以适应Python 2来打印给定时间段内的电子邮件:

#!/usr/bin/env python3
"""Print emails from a given date interval.

Usage:

    $ %(prog)s <since_date> <before_date>

since_date < before_date

Date format: DD-Mon-YYYY e.g., 3-Mar-2014

Based on http://pymotw.com/2/imaplib/
"""
import email
import sys
from datetime import datetime
from imaplib import IMAP4_SSL

import imaplib; imaplib.Debug = True

def decode_header(header_string):
    try:
        decoded_seq = email.header.decode_header(header_string)
        return str(email.header.make_header(decoded_seq))
    except Exception: # fallback: return as is
        return header_string

def get_text(msg, fallback_encoding='utf-8', errors='replace'):
    """Extract plain text from email."""
    text = []
    for part in msg.walk():
        if part.get_content_type() == 'text/plain':
            p = part.get_payload(decode=True)
            if p is not None:
                text.append(p.decode(part.get_charset() or fallback_encoding,
                                     errors))
    return "\n".join(text)


# define since/before dates
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
since_date = datetime.strptime(sys.argv[1], date_format)
before_date = datetime.strptime(sys.argv[2], date_format)

imap_host, imap_port = "imap.gmail.com", 993
login, password = 'user@gmail.com', 'password'

# connect to the imap server
mail = IMAP4_SSL(imap_host, imap_port)
mail.login(login, password)
try:
    mail.select('INBOX', readonly=True)

    # get all messages since since_date and before before_date
    typ, [msg_ids] = mail.search(None,
        '(since "%s" before "%s")' % (since_date.strftime(date_format),
                                      before_date.strftime(date_format)))

    # get complete email messages in RFC822 format
    for num in msg_ids.split():
        typ, msg_data = mail.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_bytes(response_part[1])
                for header in [ 'subject', 'to', 'from', 'date' ]:
                    print('%-8s: %s' % (
                        header.upper(), decode_header(msg[header])))
                print(get_text(msg))
finally:
    try:
        mail.close()
    finally:
        mail.logout()

请参阅imap-fetch-email-before-date.py