我想从python发送一封电子邮件:
thedoc = generate_doc()
mail.send_mail(sender="Support",
to="user@mail.co.uk",
subject="RE: ref",
attachments=('thedoc.docx', thedoc),
body="""Blah blah blah""")
以下是generate_doc()
:
zin = zipfile.ZipFile("template/thedoc.docx",'r')
zout = zipfile.ZipFile(in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
in_memory_zip = StringIO.StringIO()
for item in zin.infolist():
buffer = zin.read(item.filename)
zout.writestr(item, buffer)
# sets windows as the create system to avoid unix file permissions
for zfile in zout.filelist:
zfile.create_system = 0
zin.close()
zout.close()
in_memory_zip.seek(0)
#return in_memory_zip.getvalue() <--- This is usual return
f = file('random.docx', "w") <--- this is test to try and diagnose
f.write(in_memory_zip.getvalue()) <--- the file written here opens correctly
f.close()
电子邮件已成功发送,但附加的.docx无法在word / pages / skydrive中打开,似乎已损坏(并且尺寸略大于本地写入的文件)
如果我在本地编写文件而不是作为附件发送,则会正确打开。
在发送附件之前,是否需要模拟f.write(in_memory_zip.getvalue())
?
如果是这样,怎么样?或者我还能做些什么来诊断问题?