Changeset - 7aff9d527bf1
[Not reviewed]
default
0 2 0
Mads Kiilerich - 6 years ago 2019-12-28 17:36:09
mads@kiilerich.com
Grafted from: 87dfabe25edf
lib: fix mail address encodings and add test coverage

Now it also works on Py3 ... apparently in more cases and more correctly than
before.
2 files changed with 11 insertions and 9 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/rcmail/response.py
Show inline comments
 
@@ -343,19 +343,19 @@ def to_message(mail, separator="; "):
 
        raise EncodingError("Content-Type malformed, not allowed: %r; "
 
                            "%r (Python ERROR: %s" %
 
                            (ctype, params, exc.message))
 

	
 
    for k in mail.keys():
 
        if k in ADDRESS_HEADERS_WHITELIST:
 
            out[k.encode('ascii')] = header_to_mime_encoding(
 
            out[k] = header_to_mime_encoding(
 
                                         mail[k],
 
                                         not_email=False,
 
                                         separator=separator
 
                                     )
 
        else:
 
            out[k.encode('ascii')] = header_to_mime_encoding(
 
            out[k] = header_to_mime_encoding(
 
                                         mail[k],
 
                                         not_email=True
 
                                    )
 

	
 
    out.extract_payload(mail)
 

	
 
@@ -440,15 +440,15 @@ def properly_encode_header(value, encode
 
    addresses by changing the '@' to '-AT-'.  This is where
 
    VALUE_IS_EMAIL_ADDRESS exists.  It's a simple lambda returning True/False
 
    to check if a header value has an email address.  If you need to make this
 
    check different, then change this.
 
    """
 
    try:
 
        return value.encode("ascii")
 
    except UnicodeEncodeError:
 
        value.encode("ascii")
 
        return value
 
    except UnicodeError:
 
        if not not_email 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)
 
            return '"%s" <%s>' % (encoder.header_encode(name), address)
 

	
 
        return encoder.header_encode(value.encode("utf-8"))
 
        return encoder.header_encode(value)
kallithea/tests/other/test_mail.py
Show inline comments
 
# -*- coding: utf-8 -*-
 

	
 
import mock
 

	
 
import kallithea
 
from kallithea.model.db import User
 
from kallithea.tests import base
 

	
 
@@ -141,15 +143,15 @@ class TestMail(base.TestController):
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 

	
 
    def test_send_mail_with_author_full_mail_from(self):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = ['rcpt1', 'rcpt2']
 
        recipients = ['ræcpt1', 'receptor2 <rcpt2@example.com>', 'tæst@example.com', 'Tæst <test@example.com>']
 
        envelope_addr = 'noreply@mailserver.org'
 
        envelope_from = 'Some Name <%s>' % envelope_addr
 
        envelope_from = 'Söme Næme <%s>' % envelope_addr
 
        subject = 'subject'
 
        body = 'body'
 
        html_body = 'html_body'
 
        author = User.get_by_username(base.TEST_USER_REGULAR_LOGIN)
 

	
 
        config_mock = {
0 comments (0 inline, 0 general)