UnicodeDecodeError发送带有PDF附件的电子邮件

时间:2014-11-19 21:27:15

标签: django

我正在尝试发送带有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<<.....

1 个答案:

答案 0 :(得分:0)

这似乎是我们使用的电子邮件后端的一个问题:https://github.com/dstufft/django-postmark在发送类似的附件方面存在问题。我们搬到了mandrill,问题就消失了。