diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2019-05-27 14:27:41 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2019-06-02 14:39:15 +0100 |
commit | 9b75fec97b4ad23178a730623ba691fbe666a747 (patch) | |
tree | 706974c080126042a2ce37399e15569e9dc7c824 /alot | |
parent | 553ec461d0d77e899ae51ca67ce425b5e280d1e7 (diff) |
fix formataddr
This adds a local utility function `formataddr` which acts as the direct
inverse of `email.utils.parseaddr`.
The problem with `email.utils.formataddr` is (currently) that
- it encodes non-ascii characters and
- it does not re-introduce quotes around the real name parts in case
parseaddr removed them:
>>>parseaddr('"Ö, Ä" <a@b.c>')
>>>('Ö, Ä', 'a@b.c')
>>>formataddr(('Ö, Ä', 'a@b.c'))
>>>'=?utf-8?q?=C3=96=2C_=C3=84?= <a@b.c>'
>>>parseaddr('=?utf-8?q?=C3=96=2C_=C3=84?= <a@b.c>')
>>>('=?utf-8?q?=C3=96=2C_=C3=84?=', 'a@b.c')
related issue #1402
Diffstat (limited to 'alot')
-rw-r--r-- | alot/commands/thread.py | 3 | ||||
-rw-r--r-- | alot/db/utils.py | 14 |
2 files changed, 16 insertions, 1 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py index e3c15b2e..c4ece713 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -10,7 +10,7 @@ import subprocess import tempfile import email import email.policy -from email.utils import getaddresses, parseaddr, formataddr +from email.utils import getaddresses, parseaddr from email.message import Message import urwid @@ -26,6 +26,7 @@ from .common import RetagPromptCommand from .envelope import SendCommand from ..completion import ContactsCompleter, PathCompleter from ..db.utils import decode_header +from ..db.utils import formataddr from ..db.utils import extract_headers from ..db.utils import extract_body from ..db.envelope import Envelope diff --git a/alot/db/utils.py b/alot/db/utils.py index 75fc0178..63260b22 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -512,6 +512,20 @@ def extract_body(mail, types=None, field_key='copiousoutput'): return u'\n\n'.join(body_parts) +def formataddr(pair): + """ this is the inverse of email.utils.parseaddr: + other than email.utils.formataddr, this + - *will not* re-encode unicode strings, and + - *will* re-introduce quotes around real names containing commas + """ + name, address = pair + if not name: + return address + elif ',' in name: + name = "\"" + name + "\"" + return "{0} <{1}>".format(name, address) + + def decode_header(header, normalize=False): """ decode a header value to a unicode string |