MdSadiqMd opened a new pull request, #65875:
URL: https://github.com/apache/airflow/pull/65875

   <!-- SPDX-License-Identifier: Apache-2.0
        https://www.apache.org/licenses/LICENSE-2.0 -->
   
   Fix IMAP hook not decoding RFC 2047 encoded attachment filenames
   
   IMAP attachment filenames containing non-ASCII characters (Cyrillic, 
Chinese, etc.) are now properly decoded from RFC 2047 MIME encoding format to 
Unicode strings.
   
   Previously, filenames were returned in their raw encoded form (e.g., 
`=?UTF-8?B?0YLQtdGB0YIuY3N2?=` instead of `тест.csv`), making it impossible to:
   - Filter attachments by localized filenames
   - Match filenames using regex patterns
   - Identify files generated by external systems with non-Latin names
   
   This was particularly problematic in production environments processing 
emails from banks, ERP systems, and reporting tools that use localized 
filenames.
   
   **Changes:**
   - Added `_decode_filename()` static method to `MailPart` class that decodes 
RFC 2047 MIME-encoded filenames using Python's `email.header.decode_header()`
   - Applied decoding in all three filename access points:
     - `has_matching_name()` - for regex matching
     - `has_equal_name()` - for exact matching  
     - `get_file()` - when returning filename/payload tuples
   - Handles both encoded and plain ASCII filenames transparently
   - Fully backward compatible - existing code continues to work
   
   **Testing:**
   - Added test for exact match with Cyrillic filename (Перечень точек 
продаж_2026-04-21.xlsx)
   - Added test for `has_mail_attachment()` with encoded filename
   - Added test for regex matching with encoded filename
   - All tests use real RFC 2047 encoded strings from production scenarios
   
   **Example:**
   ```python
   # Before: required manual decoding workaround
   files = imap_hook.retrieve_mail_attachments(name='(.*)')
   for file in files:
       decoded = decode_header(file[0])  # manual decoding
       
   # After: works directly with Unicode filenames
   files = imap_hook.retrieve_mail_attachments(name='Перечень точек 
продаж_2026-04-21.xlsx')
   ```
   
   * closes: #65871 
   
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change below checkbox to `[X]` followed by the name of the tool, uncomment 
the "Generated-by".
   -->
   
   - [x] Yes - Claude Code
   Generated-by: Claude Code following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to