[Django] #32806: Send email with a content_type=text/* attachment

9 views
Skip to first unread message

Django

unread,
Jun 1, 2021, 10:40:24 AM6/1/21
to django-...@googlegroups.com
#32806: Send email with a content_type=text/* attachment
-----------------------------------------+---------------------------------
Reporter: tzot | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (Mail) | Version: 3.2
Severity: Normal | Keywords: text attachment
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+---------------------------------
Python 3.8, Django 3.2.3
{{{
from django.core.mail import EmailMessage

e = EmailMessage('subject', 'the body', 'sen...@domain.org',
['reci...@domain.org'])
e.attach('attachment.csv', 'flda,fldb\nvala,valb\n', 'text/csv')
e.send()
}}}
This fails as expected:
{{{

File "…/lib/python3.8/site-packages/sgbackend/mail.py", line 149, in
_build_sg_mail
base64_attachment = base64.b64encode(attachment[1])
File "…/lib/python3.8/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
}}}

But this also fails:
{{{
e.attach('attachment.csv', b'flda,fldb\nvala,valb\n', 'text/csv')
e.send()
}}}

because …/lib/python3.8/site-packages/django/core/mail/message.py on
attach does:

{{{
if basetype == 'text':
if isinstance(content, bytes):
try:
content = content.decode()
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's
# actually binary, read() raises a
UnicodeDecodeError.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

self.attachments.append((filename, content, mimetype))
}}}

Why decode it if a Py3 string can't be b64encoded? I think that actually
it should be *encoded* as bytes if already str using the DEFAULT_CHARSET
before appending the tuple to self.attachments.

If I .attach(name, bytes_object, 'application/csv') then the send
operation works fine.

(I'm open to the possibility that I miss something and this is not a bug
:) )

I can provide a patch if we decide upon the correct behaviour, but please
let me know the target branch.

--
Ticket URL: <https://code.djangoproject.com/ticket/32806>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 1, 2021, 10:43:26 AM6/1/21
to django-...@googlegroups.com
#32806: Send email with a content_type=text/* attachment
---------------------------------+--------------------------------------

Reporter: tzot | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (Mail) | Version: 3.2
Severity: Normal | Resolution:

Keywords: text attachment | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by tzot:

Old description:

New description:

Python 3.8.5, Django 3.2.3
{{{
from django.core.mail import EmailMessage

self.attachments.append((filename, content, mimetype))
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32806#comment:1>

Django

unread,
Jun 1, 2021, 2:08:31 PM6/1/21
to django-...@googlegroups.com
#32806: Send email with a content_type=text/* attachment
-----------------------------------+--------------------------------------
Reporter: Christos Georgiou | Owner: nobody
Type: Bug | Status: closed

Component: Core (Mail) | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: sendgrid-django | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* keywords: text attachment => sendgrid-django
* status: new => closed
* resolution: => invalid
* type: Uncategorized => Bug


Comment:

I cannot reproduce this issue with built-in backends, e.g.


{{{
>>> from django.core.mail import EmailMessage
>>> e = EmailMessage('subject', 'the body', 'sen...@domain.org',
['reci...@domain.org'])
>>> e.attach('attachment.csv', 'flda,fldb\nvala,valb\n', 'text/csv')
>>> e.send()

Content-Type: multipart/mixed;
boundary="===============7549085999326566442=="
....


>>> e = EmailMessage('subject', 'the body', 'sen...@domain.org',
['reci...@domain.org'])

>>> e.attach('attachment.csv', b'flda,fldb\nvala,valb\n', 'text/csv')
>>> e.send()

Content-Type: multipart/mixed;
boundary="===============2053797502762006814=="
....
}}}
Also `sendgrid-django` doesn't support [https://github.com/elbuo8
/sendgrid-
django/blob/aeb46bd06185b801592f276ebec8f47c7bd75b42/.travis.yml#L10-L15
Django 3.1+], you should report this issue on their bugtracker.

--
Ticket URL: <https://code.djangoproject.com/ticket/32806#comment:2>

Reply all
Reply to author
Forward
0 new messages