使用Imap4获取空白发件人电子邮件ID

时间:2017-06-15 13:27:56

标签: python django office365 imap

我正在接受来自'的标题。空白即无法获取发件人地址

c.select('INBOX', readonly=True)

for i in range(1, 30):
    typ, msg_data = c.fetch(str(i), '(RFC822)')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            for header in [ 'subject', 'to', 'from' ]:
                print '%-8s: %s' % (header.upper(), msg[header])

这是输出

主题:这是测试主题

TO:" abhi.don@gdp.gov"

FROM:

修改1

即使我没有收到消息ID,即消息ID也是空白的。 我甚至尝试了这段代码

typ, data = mb.fetch(num_id, '(BODY[HEADER.FIELDS (MESSAGE-ID)])')  
msg_str = email.message_from_string(data[0][1])  
message_id = msg_str.get('Message-ID')  
print message_id

1 个答案:

答案 0 :(得分:0)

There's no point in fetching the entire message RFC 5322 plaintext serialization to read the From field. Have a look at RFC 3501, consult the BODYSTRUCTURE field and use the data from there. Remember to apply RFC 2047 decoding to get real Unicode text.