[SDESK-7913] Handle str attachments in core email send#3240
Merged
Conversation
_get_message_from_request passed attachment data straight to add_attachment/add_related with maintype/subtype. The email library dispatches on the data type: a str routes to set_text_content(), which rejects maintype/subtype and raises a TypeError, breaking any str attachment despite EmailAttachment.data being typed str | bytes. Encode str payloads to bytes so they route through set_bytes_content, which accepts maintype/subtype and base64-encodes the part, matching the previous flask-mail behaviour. Regression introduced by the flask-mail to aiosmtplib migration (SDESK-7913).
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes the core email sending path to correctly handle EmailAttachment.data when it is provided as a str (not just bytes), preventing a TypeError during attachment handling and restoring parity with the previous flask-mail behavior.
Changes:
- Encode
strattachment payloads to UTF-8bytesbefore passing them toEmailMessage.add_attachment()/add_related(). - Add a regression test that sends an email with a
strattachment and asserts it is delivered with the expected MIME metadata and content.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
superdesk/core/emails.py |
Normalizes attachment payloads (str → UTF-8 bytes) so the stdlib email APIs take the bytes attachment path and don’t raise on maintype/subtype. |
tests/send_email_test.py |
Adds regression coverage for sending an email with a str attachment and verifying the resulting MIME part. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
petrjasek
approved these changes
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
EmailAttachment.datais typedstr | bytes, but the send path only worked forbytes. Passing astrattachment raisedTypeError: set_text_content() got an unexpected keyword argument 'maintype', breaking email delivery for that path.flask-mailaccepted both.What has changed
_get_message_from_requestnow encodesstrpayloads tobytesbefore handing them to the email library.strattachment.SDESK-7913