Skip to content
Merged
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
6 changes: 3 additions & 3 deletions emails/message.py
Original file line number Diff line number Diff line change
@@ -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_,
Expand Down Expand Up @@ -295,7 +295,7 @@ def as_string(self, message_cls=None):
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
Expand Down
13 changes: 12 additions & 1 deletion emails/signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def sign_message(self, msg):
"""
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)
Expand All @@ -74,5 +79,11 @@ def sign_message_string(self, message_string):
"""
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

27 changes: 21 additions & 6 deletions emails/testsuite/message/test_dkim.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)]))


Expand All @@ -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():
Expand Down
12 changes: 1 addition & 11 deletions emails/testsuite/message/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@


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():
Expand Down