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())
添付ファイルとして送信する前にシミュレートする必要がありますか?
もしそうなら、どのように?または、問題を診断するために他に何ができますか?