【发布时间】:2018-12-03 19:26:03
【问题描述】:
我找不到任何关于如何在 python(谷歌应用引擎)send_mail 函数中附加站点根文件夹中的文件(pdf)的示例。
url_test = "https://mywebsite.com/pdf/test.pdf"
test_file = urlfetch.fetch(url_test)
if test_file.status_code == 200:
test_document = test_file.content
mail.send_mail(sender=EMAIL_SENDER,
to=['test@test.com'],
subject=subject,
body=theBody,
attachments=[("testing",test_document)])
决定用EmailMessage试试:
message = mail.EmailMessage( sender=EMAIL_SENDER,
subject=subject,body=theBody,to=['myemail@gmail.com'],attachments=
[(attachname, blob.archivoBlob)])
message.send()
上面的 blob 附件发送成功,但是附加相对路径的文件总是显示“无效附件”
new_file = open(os.path.dirname(__file__) +
'/../pages/pdf/test.PDF').read()
message = mail.EmailMessage( sender=EMAIL_SENDER,
subject=subject,body=theBody,to=['myemail@gmail.com'],attachments=
[('testing',new_file )])
message.send()
在调试中,我还尝试通过这样做来查看文件是否正在被读取:
logging.info(new_file)
它似乎正在读取文件,因为它输出了一些 unicode 字符
请帮助我为什么不能附加 PDF 而我可以附加 blob
【问题讨论】:
标签: django python-2.7 google-app-engine