Skip to content

Conversation

@AaronDDM
Copy link
Contributor

@AaronDDM AaronDDM commented Oct 6, 2025

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-data encoding. The json.dumps() function, used to serialize the message payload, defaulted to ensure_ascii=True. This caused UTF-8 characters to be escaped as unicode sequences (e.g., \u00e9 for é), 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 the multipart/form-data encoding and email clients.

Impact:

  • Fixes: Incorrect encoding for special characters in subjects and bodies of messages and drafts when large attachments are present.
  • Supports: All international character sets (French, Spanish, German, Russian, Japanese, Chinese, etc.) and emoji.
  • Compatibility: 100% backwards compatible with no breaking changes or performance impact. Small messages (without large attachments) were unaffected and continue to work correctly.

Testing:

  • Added 6 new comprehensive tests across test_file_utils.py, test_messages.py, and test_drafts.py to cover various scenarios, including special characters in subjects/bodies for both small and large attachments, and draft creation.
  • All 68 tests (new and existing) pass, ensuring no regressions.

A new example (examples/special_characters_demo/) demonstrates the fix and proper handling of international characters.


Open in Cursor Open in Web

Co-authored-by: aaron.d <aaron.d@nylas.com>
@cursor
Copy link

cursor bot commented Oct 6, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

Co-authored-by: aaron.d <aaron.d@nylas.com>
Co-authored-by: aaron.d <aaron.d@nylas.com>
- Remove trailing whitespace on lines 92 and 98
- Rename 'json' variable to 'response_data' to avoid shadowing the imported json module
@AaronDDM AaronDDM merged commit d226ff1 into main Oct 8, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants