Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: [ '3.8', '3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand Down
23 changes: 22 additions & 1 deletion imbox/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import quopri
import time
from datetime import datetime
from email.header import decode_header
from email.header import decode_header, Header
from imbox.utils import str_encode, str_decode

import logging
Expand Down Expand Up @@ -55,6 +55,17 @@ def get_mail_addresses(message, header_name):
Retrieve all email addresses from one message header.
"""
headers = [h for h in message.get_all(header_name, [])]

clean_headers = []
# method message.get_all returns Header objects if it finds encoding errors
# but next method email.utils.getaddresses() don't work with Header objects,
# so we need to convert them to strings replacing bad chars with ???
for h in headers:
if isinstance(h, Header):
h = h.__str__().encode('ascii','replace').decode()
clean_headers.append(h)
headers = clean_headers

addresses = email.utils.getaddresses(headers)

for index, (address_name, address_email) in enumerate(addresses):
Expand Down Expand Up @@ -119,6 +130,11 @@ def parse_attachment(message_part):
# Check again if this is a valid attachment
content_disposition = message_part.get("Content-Disposition", None)
if content_disposition is not None and not message_part.is_multipart():

# if content_dispositon is type header converto to string
if isinstance(content_disposition, Header):
content_disposition = str(content_disposition)

dispositions = [
disposition.strip()
for disposition in parse_content_disposition(content_disposition)
Expand Down Expand Up @@ -232,6 +248,11 @@ def parse_email(raw_email, policy=None):
content_type = part.get_content_type()
part_maintype = part.get_content_maintype()
content_disposition = part.get('Content-Disposition', None)

# if content_disposition is type Header then convert to string
if isinstance(content_disposition, Header):
content_disposition = str(content_disposition)

if content_disposition or not part_maintype == "text":
content = part.get_payload(decode=True)
else:
Expand Down
Loading