Message.as_string now returns native string. Fixes #45

This commit is contained in:
Sergey Lavrinenko 2015-03-31 00:03:18 +03:00
parent 30eda38067
commit e698f6ee71
4 changed files with 37 additions and 21 deletions

View File

@ -1,9 +1,9 @@
# coding: utf-8
from __future__ import unicode_literals
from email.utils import getaddresses, formataddr
from email.utils import getaddresses
from .compat import (string_types, is_callable, to_bytes, formataddr as compat_formataddr, to_unicode)
from .compat import (string_types, is_callable, formataddr as compat_formataddr, to_unicode, to_native)
from .utils import (SafeMIMEText, SafeMIMEMultipart, sanitize_address,
parse_name_and_email, load_email_charsets,
encode_header as encode_header_,
@ -295,7 +295,7 @@ class MessageBuildMixin(object):
Changes:
v0.4.2: now returns bytes, not native string
"""
r = to_bytes(self.build_message(message_cls=message_cls).as_string())
r = to_native(self.build_message(message_cls=message_cls).as_string())
if self._signer:
r = self.sign_string(r)
return r

View File

@ -65,6 +65,11 @@ class DKIMSigner:
"""
Add DKIM header to email.message
"""
# py3 pydkim requires bytes to compute dkim header
# but py3 smtplib requires str to send DATA command (#
# so we have to convert msg.as_string
dkim_header = self.get_sign_header(to_bytes(msg.as_string()))
if dkim_header:
msg._headers.insert(0, dkim_header)
@ -74,5 +79,11 @@ class DKIMSigner:
"""
Insert DKIM header to message string
"""
# py3 pydkim requires bytes to compute dkim header
# but py3 smtplib requires str to send DATA command
# so we have to convert message_string
s = self.get_sign_string(to_bytes(message_string))
return s and s + message_string or message_string
return s and to_native(s) + message_string or message_string

View File

@ -1,11 +1,13 @@
# coding: utf-8
from __future__ import unicode_literals
import os
import email
import pytest
import emails
from emails import Message
from emails.compat import NativeStringIO, to_bytes, to_native
from emails.compat import NativeStringIO, to_bytes, to_native, is_py26
from emails.exc import DKIMException
from emails.utils import load_email_charsets
import emails.packages.dkim
from .helpers import common_email_data
@ -46,7 +48,8 @@ def _generate_key(length=1024):
def _check_dkim(message, pub_key=PUB_KEY):
def _plain_public_key(s):
return b"".join([l for l in s.split(b'\n') if not l.startswith(b'---')])
o = emails.packages.dkim.DKIM(message=message.as_string())
message = message.as_string()
o = emails.packages.dkim.DKIM(message=to_bytes(message))
return o.verify(dnsfunc=lambda name: b"".join([b"v=DKIM1; p=", _plain_public_key(pub_key)]))
@ -56,26 +59,38 @@ def test_dkim():
DKIM_PARAMS = [dict(key=NativeStringIO(to_native(priv_key)),
selector='_dkim',
domain='somewhere.net'),
domain='somewhere1.net'),
dict(key=priv_key,
selector='_dkim',
domain='somewhere.net'),
domain='somewhere2.net'),
# legacy key argument name
dict(privkey=priv_key,
selector='_dkim',
domain='somewhere.net'),
domain='somewhere3.net'),
]
if is_py26:
load_email_charsets()
for dkimparams in DKIM_PARAMS:
message = Message(**common_email_data())
message.dkim(**dkimparams)
# check DKIM header exist
assert message.as_message()['DKIM-Signature']
#print(__name__, "type message.as_string()==", type(message.as_string()))
assert b'DKIM-Signature: ' in message.as_string()
#print(message.as_string())
#print(type(message.as_string()))
#print(email.__file__)
#print(email.charset.CHARSETS)
#print('adding utf-8 charset...')
#email.charset.add_charset('utf-8', email.charset.BASE64, email.charset.BASE64)
#print(email.charset.CHARSETS)
assert 'DKIM-Signature: ' in message.as_string()
assert _check_dkim(message, pub_key)
#assert 0
def test_dkim_error():

View File

@ -15,18 +15,8 @@ from .helpers import common_email_data
def test_message_types():
if is_py2:
bytes_types = (str, )
native_string = (unicode, )
else:
bytes_types = (bytes, )
native_string = (str, )
m = emails.Message(**common_email_data())
print(type(m.as_string()))
#assert isinstance(m.as_message().as_string(), native_string)
assert isinstance(m.as_string(), bytes_types)
assert isinstance(m.as_string(), str)
def test_message_build():