Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

email.parseaddr just parses the first address, use email.getaddress to g... #31

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 7 additions & 4 deletions lamson/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import email
from email import encoders
from email.mime.base import MIMEBase
from email.utils import parseaddr
from email.utils import getaddresses
import sys


Expand Down Expand Up @@ -360,8 +360,11 @@ def properly_encode_header(value, encoder, not_email):
except UnicodeEncodeError:
if not_email is False and VALUE_IS_EMAIL_ADDRESS(value):
# this could have an email address, make sure we don't screw it up
name, address = parseaddr(value)
return '"%s" <%s>' % (encoder.header_encode(name.encode("utf-8")), address)
addresses = getaddresses(value.split(","))
return ", ".join([
'"%s" <%s>' % (encoder.header_encode(name.encode("utf-8")), address)
for name, address in addresses
])

return encoder.header_encode(value.encode("utf-8"))

Expand Down Expand Up @@ -492,7 +495,7 @@ def _parse_charset_header(data):
oddness = ('', eh, ed, continued)
break

if left:
if left and not re.search("^\s+$", left):
yield attempt_decoding('ascii', left)

if enc_header:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'url': 'http://pypi.python.org/pypi/lamson',
'download_url': 'http://pypi.python.org/pypi/lamson',
'author_email': '[email protected]',
'version': '1.3.4',
'version': '1.3.5',
'scripts': ['bin/lamson'],
'install_requires': ['chardet', 'jinja2', 'mock', 'nose', 'python-daemon',
'python-modargs'],
Expand Down
15 changes: 15 additions & 0 deletions tests/lamson_tests/encoding_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
from __future__ import with_statement
from nose.tools import *
import re
Expand Down Expand Up @@ -198,6 +199,20 @@ def test_guess_encoding_fails_completely():
chardet.detect.return_value = {'encoding': None, 'confidence': 0.0}
encoding.guess_encoding_and_decode('ascii', 'some data', errors='strict')

def test_decoding_drops_spaces_between_encoded_words():
expected = u"Vill bli borttagen från hitta.se"
actual = encoding.properly_decode_header('=?iso-8859-1?Q?Vill_bli_b?= =?iso-8859-1?Q?orttagen_f?= =?iso-8859-1?Q?r=E5n_hitta.?= =?iso-8859-1?Q?se?=')
assert_equal(actual, expected)

actual = encoding.properly_decode_header('Vill bli =?iso-8859-1?Q?borttagen_f?= =?iso-8859-1?Q?r=E5n_hitta.?= =?iso-8859-1?Q?se?=')
assert_equal(actual, expected)

actual = encoding.properly_decode_header('=?iso-8859-1?Q?Vill_bli?= borttagen =?iso-8859-1?Q?fr=E5n_hitta.?= =?iso-8859-1?Q?se?=')
assert_equal(actual, expected)

actual = encoding.properly_decode_header('=?iso-8859-1?Q?Vill_bli?= borttagen =?iso-8859-1?Q?fr=E5n_hitta.?= =?iso-8859-1?Q?se?= slut')
assert_equal(actual, u"Vill bli borttagen från hitta.se slut")


def test_attach_text():
mail = encoding.MailBase()
Expand Down