Investigate and fix email subject encoding issue #442
Merged
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.
https://nylas.atlassian.net/browse/CUST-4666
License
I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.
Fix: Email Subject/Body Encoding for Special Characters with Large Attachments
This PR resolves an issue where special characters (e.g., accented letters) in email subjects and bodies were incorrectly encoded, particularly for messages and drafts with large attachments (>3MB) sent to recipients like Gmail.
Problem:
When sending emails with large attachments, the SDK uses
multipart/form-dataencoding. Thejson.dumps()function, used to serialize the message payload, defaulted toensure_ascii=True. This caused UTF-8 characters to be escaped as unicode sequences (e.g.,\u00e9foré), leading to garbled text like "De l’idée à la post-prod, sans friction" instead of "De l'idée à la post-prod, sans friction".Solution:
The fix involves a single-line change in
nylas/utils/file_utils.py:message_payload = json.dumps(request_body, ensure_ascii=False)By setting
ensure_ascii=False, UTF-8 characters are preserved in their original form within the JSON payload, which is then correctly handled by themultipart/form-dataencoding and email clients.Impact:
Testing:
test_file_utils.py,test_messages.py, andtest_drafts.pyto cover various scenarios, including special characters in subjects/bodies for both small and large attachments, and draft creation.A new example (
examples/special_characters_demo/) demonstrates the fix and proper handling of international characters.