【发布时间】:2014-11-19 21:27:15
【问题描述】:
我正在尝试发送带有 pdf 附件的电子邮件,但在执行 message.send() 时收到 UnicodeDecodeError。我怎样才能防止这种情况发生? PDF 是使用django-easy-pdf 生成的
这是python 2.7.6。
相关代码:
pdf = open("stuff.pdf").read()
message = EmailMessage(
subject="This is a subject",
body="This is your pdf",
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email])
message.attach('stuff.pdf', pdf, 'application/pdf')
message.send(fail_silently=False)
还有例外:
UnicodeDecodeError('utf8', '%PDF-1.4\r\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.reportlab.com\r\n1 0 obj\r\n<< /F1 2 0 R /F2 4 0 R >>\r\nendobj\r\n2 0 obj\r\n<< /BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >>\r\nendobj\r\n3 0 obj\r\n<<.....
【问题讨论】:
-
这是什么 Python 版本?
-
@SimeonVisser 更新问题。
-
你能试试
message.attach_file('stuff.pdf')吗? -
我可以试一试。我得为它创建一个临时文件,真的没有.pdf文件,都是生成的。
-
使用 "with open(...) as f: ..."
标签: django