Skip to content

Commit

Permalink
fix _decode_value for unknown encoding; fix _parse_email_address
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Jan 11, 2018
1 parent d5919b3 commit 80783a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions imap_tools/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ def _decode_value(value, encoding):
if encoding in ['utf-8', None]:
return value.decode('utf-8', 'ignore')
else:
return value.decode(encoding)
try:
return value.decode(encoding)
except LookupError: # unknown encoding
return value.decode('utf-8', 'ignore')
return value

@property
Expand Down Expand Up @@ -274,6 +277,7 @@ def _parse_email_address(address: str) -> dict:
@:return dict(name: str, email: str, full: str)
"""
address = ''.join(char for char in address if char.isprintable())
address = re.sub('[\n\r\t]+', ' ', address)
result = dict(email='', name='', full='')
if '<' in address and '>' in address:
match = re.match('(?P<name>.*)?(?P<email><.*>)', address, re.UNICODE)
Expand All @@ -288,8 +292,7 @@ def _parse_email_address(address: str) -> dict:
@property
def from_values(self) -> dict:
"""The address of the sender (all data)"""
from_header_cleaned = re.sub('[\n\r\t]+', ' ', self.obj['from'])
msg_from = decode_header(from_header_cleaned)
msg_from = decode_header(self.obj['from'])
msg_txt = ''.join(self._decode_value(part[0], part[1]) for part in msg_from)
return self._parse_email_address(msg_txt)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='imap_tools',
version='0.3',
version='0.4',
packages=['imap_tools'],
url='https://github.com/ikvk/imap_tools',
license='MIT',
Expand Down

0 comments on commit 80783a0

Please sign in to comment.