OTOH, `attach_file()` will in that case first try to decode the file's
content but fall back on treating the attachment as binary, setting the
Content-Type to `application/octet-stream`.
I think the fallback provided by `attach_file()` is useful and I would
like to extend `attach()` to that same behavior.
--
Ticket URL: <https://code.djangoproject.com/ticket/27007>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I will shortly create a pull request with my proposed changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:1>
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:2>
Comment (by timgraham):
Might be a duplicate of #26802 which was committed just a couple weeks
ago.
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:3>
Comment (by Feuermurmel):
Pull request: https://github.com/django/django/pull/7014
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:4>
Comment (by Feuermurmel):
Replying to [comment:3 timgraham]:
Sorry for not mentioning #26802. That change did improve the situation,
allowing `bytes` objects. But it tries to unconditionally decode it as
UTF-8 (compared to the logic in `attach_file()`, which falls back to
treating it as a binary file). This means that if a `bytes` object is
passed which does not contain valid UTF-8, a `UnicodeDecodeError` will be
thrown.
With the changes in the pull request, the behavior is the same as when
attaching a non-UTF-8 file.
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:5>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:6>
Comment (by Claude Paroz <claude@…>):
In [changeset:"6fe391d437cdf9866c34e30b2f3831e9d12e637b" 6fe391d4]:
{{{
#!CommitTicketReference repository=""
revision="6fe391d437cdf9866c34e30b2f3831e9d12e637b"
Refs #27007 -- Enhanced mail text attachment test
The test now also checks whether the sent message's attachment has the
expected
name, content and mime type.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:7>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
Comment:
Pending some small documentation edits.
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:8>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"72d541b61cd7b0a14f70242e2207fdb3f600c4d5" 72d541b]:
{{{
#!CommitTicketReference repository=""
revision="72d541b61cd7b0a14f70242e2207fdb3f600c4d5"
Fixed #27007 -- Handled non-UTF-8 bytes objects for text/* attachments.
The fallback logic which allows non-UTF-8 encoded files to be passed to
attach_file() even when a `text/*` mime type has been specified is
moved to attach(). Both functions now fall back to a content type of
`application/octet-stream`.
A side effect is that a file's content is decoded in memory instead of
opening it in text mode and reading it into a string.
Some mimetype-related logic in _create_attachment() has become
obsolete as the code moved from attach_file() to attach() already
handles this.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27007#comment:9>