【问题标题】:UnicodeDecodeError sending an email with PDF attachmentUnicodeDecodeError 发送带有 PDF 附件的电子邮件
【发布时间】: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


【解决方案1】:

这似乎是我们使用的电子邮件后端的问题:https://github.com/dstufft/django-postmark 在发送此类附件时出现问题。我们已经转移到山魈,问题就解决了。

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 2020-11-20
    • 2014-02-28
    • 1970-01-01
    • 2021-09-14
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多