如何使用python smtplib保存附加到另一个电子邮件的电子邮件



我正在使用python imaplib下载附件并将其保存在电子邮件中。但是,当有一封带有附件的电子邮件作为另一封电子邮件时,x.get_payload() 属于 Nonetype。我认为这些类型的邮件是使用某些电子邮件客户端发送的。由于缺少文件名,我尝试更改标题中的文件名,后跟"内容处置"。重命名的文件被打开,当我尝试使用

fp.write(part.get_payload(decode=True))

它说字符串或缓冲区预期,但找不到 Nonetype。

>>>x.get_payload()
[<email.message.Message instance at 0x7f834eefa0e0>]
>>>type(part.get_payload())
<type 'list'>
>>>type(part.get_payload(decode=True))
<type 'NoneType'>

删除了解码=真,我得到了一个对象列表

x.get_payload()[0]
<email.message.Message instance at 0x7f834eefa0e0>

我尝试编辑文件名,以防电子邮件作为附件找到。

if part.get('Content-Disposition'): 
    attachment = str(part.get_filename()) #get filename
    if attachment == 'None':
        attachment = 'somename.mail'
        attachment = self.autorename(attachment)#append (no: of occurences) to filename eg:filename(1) in case file exists
        x.add_header('Content-Disposition', 'attachment', filename=attachment)
        attachedmail = 1
 if attachedmail == 1:
     fp.write(str(x.get_payload()))
 else:
     fp.write(x.get_payload(decode=True)) #write contents to the opened file

并且该文件包含对象名称文件内容如下

[ < email.message.Message instance at 0x7fe5e09aa248 > ]

如何将这些附加电子邮件的内容写入文件?

我自己解决了。 因为 [

相关内容

最新更新