Skip to content
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
1 change: 1 addition & 0 deletions emails/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#: Python 3.4.x
is_py34 = (is_py3 and _ver[1] == 4)
is_py34_plus = (is_py3 and _ver[1] >= 4)

#: Python 2.7.x
is_py27 = (is_py2 and _ver[1] == 7)
Expand Down
13 changes: 8 additions & 5 deletions emails/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

import time
from functools import wraps
from email.utils import formatdate, getaddresses

from dateutil.parser import parse as dateutil_parse
from email.header import Header
from email.utils import formatdate, getaddresses
from emails.compat import string_types, to_unicode, is_callable, to_bytes, to_native

from .compat import (string_types, is_callable, to_bytes)
from .utils import (SafeMIMEText, SafeMIMEMultipart, sanitize_address,
parse_name_and_email, load_email_charsets,
encode_header as encode_header_)
from .exc import BadHeaderError
from .backend import ObjectFactory
from .backend.smtp import SMTPBackend
from .backend import ObjectFactory, SMTPBackend
from .store import MemoryFileStore, BaseFile
from .signers import DKIMSigner


load_email_charsets() # sic!


Expand Down Expand Up @@ -53,6 +53,7 @@ class BaseMessage(object):

attachment_cls = BaseFile
filestore_cls = MemoryFileStore
policy = None

def __init__(self,
charset=None,
Expand Down Expand Up @@ -218,6 +219,8 @@ def _build_message(self, message_cls=None):

message_cls = message_cls or SafeMIMEMultipart
msg = message_cls()
if self.policy:
msg.policy = self.policy

msg.preamble = self.ROOT_PREAMBLE

Expand Down
25 changes: 23 additions & 2 deletions emails/testsuite/message/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from dateutil.parser import parse as dateutil_parse
import pytest
import emails
from emails import Message
import emails.exc
from emails.compat import to_unicode, StringIO, is_py2, is_py3
from emails.compat import to_unicode, StringIO, is_py2, is_py34_plus
from .helpers import common_email_data


Expand Down Expand Up @@ -80,4 +81,24 @@ def test_sanitize_header():
print('header {0}'.format(header))
emails.Message(html='...', **{header: value}).as_message()

# TODO: more tests here

def test_message_policy():

if is_py34_plus:

def gen_policy(**kw):
import email.policy
return email.policy.SMTP.clone(**kw)

# Generate without policy
m1 = emails.Message(**common_email_data())
m1.policy = None
# Just generate without policy
m1.as_string()

# Generate with policy
m1 = emails.Message(**common_email_data())
m1.policy = gen_policy(max_line_length=60)
# WTF: This check fails.
# assert max([len(l) for l in m1.as_string().split(b'\n')]) <= 60
# TODO: another policy checks