如何将邮件转换为 eml?

问题描述

邮件转换为 EML?

我有一台服务器,我想将其中的邮件转换为 EML 以进行备份

如何做到这一点?

尝试了以下方法

import imaplib
import getpass
import argparse

argparser = argparse.ArgumentParser(description="Dump a IMAP folder into .eml files")
argparser.add_argument('-s',dest='host',help="IMAP host,like imap.gmail.com",default= 'mail..nl')
argparser.add_argument('-u',dest='username',help="IMAP username",default= 'e@.nl')
argparser.add_argument('-r',dest='remote_folder',help="Remote folder to download",default='INBox.html')
argparser.add_argument('-l',dest='local_folder',help="Local folder where to save .eml files",default='.')
args = argparser.parse_args()

gmail = imaplib.IMAP4_SSL(args.host)
gmail.login(args.username,password1)
gmail.select(args.remote_folder)
typ,data = gmail.search(None,'ALL')
for num in data[0].split():
    typ,data = gmail.fetch(num,'(RFC822)')
    f = open('%sand%s  .eml' %(args.local_folder,num),'w')
    print(data[0][1],file=f)
    f.close()
gmail.close()
gmail.logout()

以上工作正常,但打开文件时没有输出

也试过这个:

import os
cwd = os.getcwd()
outfile_name = os.path.join(cwd,'message.eml')

class Gen_Emails(object):    
    def SavetoFile(self,msg):
        with open(outfile_name,'w') as outfile:
            gen = generator.Generator(outfile)
            gen.flatten(msg)

with MailBox('mail.yourubl.nl').login('login.nl','pwd',initial_folder='INBox') as mailBox:
    for msg in mailBox.fetch():
        SavetoFile(msg)

导致错误:AttributeError: 'MailMessage' 对象没有属性 'policy'

请帮忙!

解决方法

你必须学习python,以及一些算法书籍。

https://github.com/ikvk/imap_tools/blob/master/examples/email_to_file.py

您似乎试图复制/粘贴而不是编程。

我不想做坏事。