'bytes' 对象在 EmailMessage 中没有属性 'encode'

'bytes' object has no attribute 'encode' in EmailMessage

我正在尝试在 Django 中制作一个应用程序,它接受来自用户的信息并通过电子邮件将 HTML 模板作为 pdf 发送给用户。但我收到这个错误 'bytes'对象没有属性'encode'

这是我对邮件的看法

def email(request, serial_no):
    user = get_object_or_404(Student, pk=serial_no)
    # roll_no = {'roll': str(user['roll_no'])}
    html = render_to_string('card.html',
                            {'user': user})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename={}'.format(user.roll_no + '.pdf')
    pdf = weasyprint.HTML(string=html, base_url='').write_pdf(
        stylesheets=[weasyprint.CSS(string='body { font-family: serif}')])
    to_emails = [str(user.roll_no) + '@gmail.com']
    subject = "Certificate from Nami Montana"
    email = EmailMessage(subject, body=pdf, from_email='SSC', to=to_emails)
    email.attach("{}".format(user.roll_no) + '.pdf', pdf, "application/pdf")
    email.content_subtype = "pdf"  # Main content is now text/html
    email.encoding = 'utf-8'
    email.send()
    return HttpResponseRedirect(reverse('id_card:submit'))

这里是错误

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Dell\Documents\GitHub\SSC-Website\id_card\views.py", line 49, in email
    email.send()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\console.py", line 34, in send_messages
    self.write_message(message)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\console.py", line 17, in write_message
    msg = message.message()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 246, in message
    msg = SafeMIMEText(self.body, self.content_subtype, encoding)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 159, in __init__
    MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\email\mime\text.py", line 42, in __init__
    self.set_payload(_text, _charset)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 167, in set_payload
    has_long_lines = any(
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 168, in <genexpr>
    len(line.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT
AttributeError: 'bytes' object has no attribute 'encode'

提前致谢

问题是您发送的是 PDF 文件对象作为邮件正文:

pdf = weasyprint.HTML(string=html, base_url='').write_pdf(
        stylesheets=[weasyprint.CSS(string='body { font-family: serif}')])
email = EmailMessage(subject, body=pdf, from_email='SSC', to=to_emails)

EmailMessage 尝试使用 utf-8 编解码器对 body 进行编码,但正文是一个 PDF 文件对象,即 bytes,而 bytes 不会' 仅提供 encode 方法和 decode 方法。将 PDF 添加到电子邮件的正确方法是附加它,就像您稍后在代码中所做的那样。现在,如果您希望电子邮件正文成为 PDF 文件的文本,请在 str 中获取文本并将其作为 body kwarg 传递。