#36119: Attaching email file to email fails if the attachment is using 8bit
Content-Transfer-Encoding
---------------------------+---------------------------------------
     Reporter:  Trenton H  |                     Type:  Bug
       Status:  new        |                Component:  Core (Mail)
      Version:  5.1        |                 Severity:  Normal
     Keywords:             |             Triage Stage:  Unreviewed
    Has patch:  0          |      Needs documentation:  0
  Needs tests:  0          |  Patch needs improvement:  0
Easy pickings:  0          |                    UI/UX:  0
---------------------------+---------------------------------------
 If the attached email file is attached to an email, such as the code
 snipped below, the sending will fail due to the presence of non-ASCII
 content in the attachment.

 {{{#!python
 email = EmailMessage(
     subject="subject",
     body="body",
     to="[email protected]",
 )
 email.attach_file(original_file)
 n_messages = email.send()
 }}}

 {{{
 Traceback (most recent call last):
   File "/usr/src/paperless/src/documents/signals/handlers.py", line 989,
 in email_action
     n_messages = email.send()
                  ^^^^^^^^^^^^
   File "/usr/local/lib/python3.12/site-
 packages/django/core/mail/message.py", line 301, in send
     return self.get_connection(fail_silently).send_messages([self])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.12/site-
 packages/django/core/mail/backends/smtp.py", line 136, in send_messages
     sent = self._send(message)
            ^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.12/site-
 packages/django/core/mail/backends/smtp.py", line 156, in _send
     from_email, recipients, message.as_bytes(linesep="\r\n")
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.12/site-
 packages/django/core/mail/message.py", line 148, in as_bytes
     g.flatten(self, unixfrom=unixfrom, linesep=linesep)
   File "/usr/local/lib/python3.12/email/generator.py", line 117, in
 flatten
     self._write(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 182, in _write
     self._dispatch(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 219, in
 _dispatch
     meth(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 286, in
 _handle_multipart
     g.flatten(part, unixfrom=False, linesep=self._NL)
   File "/usr/local/lib/python3.12/email/generator.py", line 117, in
 flatten
     self._write(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 182, in _write
     self._dispatch(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 219, in
 _dispatch
     meth(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 372, in
 _handle_message
     g.flatten(msg.get_payload(0), unixfrom=False, linesep=self._NL)
   File "/usr/local/lib/python3.12/email/generator.py", line 117, in
 flatten
     self._write(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 182, in _write
     self._dispatch(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 219, in
 _dispatch
     meth(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 446, in
 _handle_text
     super(BytesGenerator,self)._handle_text(msg)
   File "/usr/local/lib/python3.12/email/generator.py", line 263, in
 _handle_text
     self._write_lines(payload)
   File "/usr/local/lib/python3.12/email/generator.py", line 156, in
 _write_lines
     self.write(line)
   File "/usr/local/lib/python3.12/email/generator.py", line 420, in write
     self._fp.write(s.encode('ascii', 'surrogateescape'))

                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  UnicodeEncodeError: 'ascii' codec can't encode characters in position
 10-15: ordinal not in range(128)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36119>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194860852d5-f72144ef-a831-464f-924c-437c85bf1b2e-000000%40eu-central-1.amazonses.com.

Reply via email to