summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Lavrinenko <s@lavr.me>2015-03-30 21:03:18 (GMT)
committerSergey Lavrinenko <s@lavr.me>2015-03-31 09:18:50 (GMT)
commite698f6ee71c0757f8747916675c7d43a839397b0 (patch)
tree04381d41225ce87dc26a6c8c46a6cc76057bf28a
parent30eda3806792e55d5c86f05ef532cd4e143281e3 (diff)
downloadpython-emails-e698f6ee71c0757f8747916675c7d43a839397b0.zip
python-emails-e698f6ee71c0757f8747916675c7d43a839397b0.tar.gz
python-emails-e698f6ee71c0757f8747916675c7d43a839397b0.tar.bz2
Message.as_string now returns native string. Fixes #45
-rw-r--r--emails/message.py6
-rw-r--r--emails/signers.py13
-rw-r--r--emails/testsuite/message/test_dkim.py27
-rw-r--r--emails/testsuite/message/test_message.py12
4 files changed, 37 insertions, 21 deletions
diff --git a/emails/message.py b/emails/message.py
index cbf0709..d059267 100644
--- a/emails/message.py
+++ b/emails/message.py
@@ -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
diff --git a/emails/signers.py b/emails/signers.py
index 390362e..e41c26e 100644
--- a/emails/signers.py
+++ b/emails/signers.py
@@ -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
+
diff --git a/emails/testsuite/message/test_dkim.py b/emails/testsuite/message/test_dkim.py
index e2b5b54..4a781ca 100644
--- a/emails/testsuite/message/test_dkim.py
+++ b/emails/testsuite/message/test_dkim.py
@@ -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():
diff --git a/emails/testsuite/message/test_message.py b/emails/testsuite/message/test_message.py
index 1769bb6..7384852 100644
--- a/emails/testsuite/message/test_message.py
+++ b/emails/testsuite/message/test_message.py
@@ -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():