martinrusev / imbox

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

attachments parser got ValueError #198

Open mr-pounds opened 3 years ago

mr-pounds commented 3 years ago

When I try to get message, I got ValueError.

Traceback (most recent call last): File "myimap.py", line 70, in result = obj.get_emails(date_gt='2021-03-17') File "myimap.py", line 42, in getemails , message = all_inbox_messages[index] File "C:\Users\admin\anaconda3\envs\grades\lib\site-packages\imbox\messages.py", line 77, in getitem return uid, self._fetch_email(uid) File "C:\Users\admin\anaconda3\envs\grades\lib\site-packages\imbox\messages.py", line 42, in _fetch_email return fetch_email_by_uid(uid=uid, File "C:\Users\admin\anaconda3\envs\grades\lib\site-packages\imbox\parser.py", line 156, in fetch_email_by_uid email_object = parse_email(raw_email, policy=parser_policy) File "C:\Users\admin\anaconda3\envs\grades\lib\site-packages\imbox\parser.py", line 213, in parse_email attachment = parse_attachment(part) File "C:\Users\admin\anaconda3\envs\grades\lib\site-packages\imbox\parser.py", line 123, 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: ''

I found 【s_name = ['file_name', '']】, so I add some code.

if s_name[0] == 'filename':
    # If this is a split file name - use the number after the * as an index to insert this part
    if len(s_name) > 1 and 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)

I don't kown why s_name has '', but it's working.

allanjonis commented 3 years ago

Thank you, your code solve the problem here, apparently the cause is character encoding.

kelwin-fc commented 2 years ago

I added and s_name[1].isdigit() to the same line. I guess it's slightly more robust in case s_name[1] has a non-empty string that isn't a number.