martinrusev / imbox

Python IMAP for Human beings
MIT License
1.18k stars 190 forks source link

ValueError in parse_attachment #194

Closed Munwalker closed 4 years ago

Munwalker commented 4 years ago

Hi,

I came accross a ValueError during the parsing of a mail with attachement.

File "/Scripts/Sources/attachment_downloader/venv/lib/python3.7/site-packages/imbox/messages.py", line 55, in _fetch_email_list
    yield uid, self._fetch_email(uid)
  File "/Scripts/Sources/attachment_downloader/venv/lib/python3.7/site-packages/imbox/messages.py", line 44, in _fetch_email
    parser_policy=self.parser_policy)
  File "/Scripts/Sources/attachment_downloader/venv/lib/python3.7/site-packages/imbox/parser.py", line 155, in fetch_email_by_uid
    email_object = parse_email(raw_email, policy=parser_policy)
  File "/Scripts/Sources/attachment_downloader/venv/lib/python3.7/site-packages/imbox/parser.py", line 212, in parse_email
    attachment = parse_attachment(part)
  File "/Scripts/Sources/attachment_downloader/venv/lib/python3.7/site-packages/imbox/parser.py", line 122, in parse_attachment
    filename_parts.insert(int(s_name[1]),value[1:-1] if value.startswith('"') else value)
ValueError: invalid literal for int() with base 10: ''

Error when inserting filename part, here :

# Check for split filename
 s_name = name.split("*")
 if s_name[0] == 'filename':
 # this is a split file name - use the number after the * as an index to insert this part
     if len(s_name) > 1:
          filename_parts.insert(int(s_name[1]),value[1:-1] if value.startswith('"') else value)
     else:
          filename_parts.insert(0,value[1:-1] if value.startswith('"') else value)
Munwalker commented 4 years ago

Just saw that the name (imbox/parser.py line 115) have a * at the end.

['inline', "filename*=utf-8''TEST%20mailing%20BA%203%2D10%E2%82%AC%20TEST%2008%2D2020%5FMise%20en%20page%201.pdf"]`

this cause the array s_name (imbox/parser.py line 119) to have an empty element. which then cause the insert (imbox/parser.py line 122) to fail.

I suggest that we remove empty element from this array :

                    s_name = list(filter(None, name.split("*")))
Munwalker commented 4 years ago

Just saw that this was fixed but not released:

s_name = name.rstrip('*').split("*")