我正在用 Python 开发电子邮件客户端应用程序。
如何使用 Imap 获取具有 .eml 附件的邮件正文?
下面的代码完成了这项工作。您需要 python2.7 来运行以下代码,因为这是在过去编码的。但是,您的问题也来自过去,所以我想这很好。该代码非常简单,因此请看一下。
from email import message_from_file
import os
def file_exists (f):
return os.path.exists(os.path.join(path, f).replace("\\","/"))
def save_file (fn, cont):
file = open(os.path.join(path, fn).replace("\\","/"), "wb")
file.write(cont)
file.close()
def construct_name (id, fn):
id = id.split(".")
id = id[0]+id[1]
return id+"."+fn
def disqo (s):
s = s.strip()
if s.startswith("'") and s.endswith("'"): return s[1:-1]
if s.startswith('"') and s.endswith('"'): return s[1:-1]
return s
def disgra (s):
s = s.strip()
if s.startswith("<") and s.endswith(">"): return s[1:-1]
return s
def pullout (m, key):
Html = ""
Text = ""
Files = {}
Parts = 0
if not m.is_multipart():
if m.get_filename():
fn = m.get_filename()
cfn = construct_name(key, fn)
Files[fn] = (cfn, None)
if file_exists(cfn): return Text, Html, Files, 1
save_file(cfn, m.get_payload(decode=True))
return Text, Html, Files, 1
cp = m.get_content_type()
if cp=="text/plain": Text += m.get_payload(decode=True)
elif cp=="text/html": Html += m.get_payload(decode=True)
else:
cp = m.get("content-type")
try: id = disgra(m.get("content-id"))
except: id = None
o = cp.find("name=")
if o==-1: return Text, Html, Files, 1
ox = cp.find(";", o)
if ox==-1: ox = None
o += 5; fn = cp[o:ox]
fn = disqo(fn)
cfn = construct_name(key, fn)
Files[fn] = (cfn, id)
if file_exists(cfn): return Text, Html, Files, 1
save_file(cfn, m.get_payload(decode=True))
return Text, Html, Files, 1
y = 0
while 1:
try:
pl = m.get_payload(y)
except: break
t, h, f, p = pullout(pl, key)
Text += t; Html += h; Files.update(f); Parts += p
y += 1
return Text, Html, Files, Parts
def extract (msgfile, key):
m = message_from_file(msgfile)
From, To, Subject, Date = caption(m)
Text, Html, Files, Parts = pullout(m, key)
Text = Text.strip(); Html = Html.strip()
msg = {"subject": Subject, "from": From, "to": To, "date": Date,
"text": Text, "html": Html, "parts": Parts}
if Files: msg["files"] = Files
return msg
def caption (origin):
Date = ""
if origin.has_key("date"): Date = origin["date"].strip()
From = ""
if origin.has_key("from"): From = origin["from"].strip()
To = ""
if origin.has_key("to"): To = origin["to"].strip()
Subject = ""
if origin.has_key("subject"): Subject = origin["subject"].strip()
return From, To, Subject, Date
if __name__ == "__main__":
global path
startdirname = "Email"
num = 1
for i in range(10000000):
if os.path.exists(startdirname + str(num)) == False:
os.makedirs("Email" + str(num))
break
else:
num += 1
for i in os.listdir("."):
if i.endswith(".eml") == True:
nam = i[:-4]
path = "./" + startdirname + str(num) + "/" + nam
os.makedirs("./" + startdirname + str(num) + "/" + nam)
f = open(i, "rb")
emailDict = extract(f, f.name)
f.close()
textFile = ""
froms = emailDict["from"]
tos = emailDict["to"]
subject = emailDict["subject"]
parts = emailDict["parts"]
date = emailDict["date"]
txt = emailDict["text"]
html = emailDict["html"]
files = []
for i in emailDict["files"]:
files.append(i)
textFile += "From: " + froms + "\n"
textFile += "To: " + tos + "\n"
textFile += "Subject: " + subject + "\n"
textFile += "Date: " + date + "\n\n"
textFile += "Files: " + ", ".join(files) + "\n"
textFile += "Parts: " + str(parts) + "\n\n"
textFile += "Text:\n\n" + txt + "\n\n"
textFile += "HTML:\n\n" + html
wf = open("./" + startdirname + str(num) + "/" + nam + "/" + "txt_" + nam + ".txt", "w")
wf.write(textFile)
wf.close()