vaefremov95 opened a new pull request, #62321:
URL: https://github.com/apache/airflow/pull/62321
Hello!
While testing new feature max_mails, that was released in #60963, I noticed
unexpected behavior in `imap_hook.download_mail_attachments` method . It
overwrites existing files if multiple attachments have the same filename.
For example, if 3 attachments returned by `mail_filter` share the same name,
the `retrieve_mail_attachments` method correctly processes all of them.
However, `download_mail_attachments` overwrites the files, resulting in only
one file being saved instead of three.
Logs:
```
[2026-02-13, 22:43:03 UTC] {imap.py:336} INFO - Found attachment: test.xlsx
[2026-02-13, 22:43:03 UTC] {imap.py:336} INFO - Found attachment: test.xlsx
[2026-02-13, 22:43:03 UTC] {imap.py:336} INFO - Found attachment: test.xlsx
[2026-02-13, 22:43:03 UTC] {logging_mixin.py:190} INFO - downloaded
attachments without max_mails: 1
```
<img width="391" height="194" alt="image"
src="https://github.com/user-attachments/assets/74d86d34-23bf-45f8-a895-6ae10c60b201"
/>
This PR introduces a new flag `overwrite_file`, which defaults to `True` to
preserve the existing behavior.
- If `overwrite_file=True`, files are overwritten as before.
- If `overwrite_file=False`, new files with incremental suffixes are created
instead of overwriting existing ones.
<img width="158" height="90" alt="image"
src="https://github.com/user-attachments/assets/dfd83d17-8ce5-49e5-b24e-8f23a0fe36e5"
/>
To achieve this behavior, I modified the internal `_create_file` method and
introduced a new method `_get_unique_path` to generate unique filenames when
needed.
The following unit tests were added and are passing:
- Test overwrite behavior when `overwrite_file=True`
- Test unique file creation when `overwrite_file=False`
- Test multiple attachments with identical names
- Test incremental suffix creation (`_1`, `_2`, etc.)
##### 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 (please specify the tool below)
Generated-by: [GPT-5] 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]