diff options
author | Lucas Hoffmann <l-m-h@web.de> | 2017-06-30 11:00:38 +0200 |
---|---|---|
committer | Lucas Hoffmann <l-m-h@web.de> | 2017-06-30 11:03:43 +0200 |
commit | 1e0595d39263d9f19d818c949d6e593e70303672 (patch) | |
tree | b6f018efedbce3bf374dc06d7710a1ae270e7f9f /alot | |
parent | 1c7565783ac93f4e627ab8d3f7c14762c807c7e5 (diff) |
Use stdlib functions in db.utils.encode_header
Instead of manual parsing with regexp and manual string formatting the
functions from email.utils are used. This fixes some small
inconsistencies with addresses with empty realnames and with commas in
realnames.
Diffstat (limited to 'alot')
-rw-r--r-- | alot/db/utils.py | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/alot/db/utils.py b/alot/db/utils.py index e4a2d2e0..2bcf6aca 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -8,6 +8,7 @@ import email import email.charset as charset from email.header import Header from email.iterators import typed_subpart_iterator +import email.utils import tempfile import re import logging @@ -398,17 +399,14 @@ def encode_header(key, value): """ # handle list of "realname <email>" entries separately if key.lower() in ['from', 'to', 'cc', 'bcc']: - rawentries = value.split(',') + rawentries = email.utils.getaddresses([value]) encodedentries = [] - for entry in rawentries: - m = re.search(r'\s*(.*)\s+<(.*\@.*\.\w*)>\s*$', entry) - if m: # If a realname part is contained - name, address = m.groups() - # try to encode as ascii, if that fails, revert to utf-8 - # name must be a unicode string here - namepart = Header(name) - # append address part encoded as ascii - entry = '%s <%s>' % (namepart.encode(), address) + for name, address in rawentries: + # try to encode as ascii, if that fails, revert to utf-8 + # name must be a unicode string here + namepart = Header(name) + # append address part encoded as ascii + entry = email.utils.formataddr((namepart.encode(), address)) encodedentries.append(entry) value = Header(', '.join(encodedentries)) else: |