From 3659d0f2f261c40a2a73534b460406ec82bf1ed5 Mon Sep 17 00:00:00 2001 From: Daniel Holth Date: Wed, 4 Mar 2020 18:09:53 -0500 Subject: [PATCH 1/2] Ensure CRLF line endings when sending email Untested. Fixes #46 ? --- repoze/sendmail/encoding.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/repoze/sendmail/encoding.py b/repoze/sendmail/encoding.py index 03fff54..d588da0 100644 --- a/repoze/sendmail/encoding.py +++ b/repoze/sendmail/encoding.py @@ -1,5 +1,6 @@ from email import utils from email import header +from email.policy import SMTP from repoze.sendmail._compat import PY_2 from repoze.sendmail._compat import text_type @@ -100,7 +101,8 @@ def encode_message(message, The return is a byte string of the whole message. """ cleanup_message(message) - return message.as_string().encode('ascii') + return message.as_string(policy=SMTP).encode('ascii') + # Or messsage.as_bytes(policy=SMTP) ? def best_charset(text): From a906baa500484481f1401e24879a80eca3ca5062 Mon Sep 17 00:00:00 2001 From: Daniel Holth Date: Thu, 5 Mar 2020 08:56:30 -0500 Subject: [PATCH 2/2] use as_bytes / fallback for Python 2 --- repoze/sendmail/encoding.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/repoze/sendmail/encoding.py b/repoze/sendmail/encoding.py index d588da0..23a85b7 100644 --- a/repoze/sendmail/encoding.py +++ b/repoze/sendmail/encoding.py @@ -1,6 +1,10 @@ from email import utils from email import header -from email.policy import SMTP + +try: + from email.policy import SMTP +except ImportError: + SMTP = None from repoze.sendmail._compat import PY_2 from repoze.sendmail._compat import text_type @@ -101,8 +105,10 @@ def encode_message(message, The return is a byte string of the whole message. """ cleanup_message(message) - return message.as_string(policy=SMTP).encode('ascii') - # Or messsage.as_bytes(policy=SMTP) ? + if SMTP: # Python 3.3+ + return message.as_bytes(policy=SMTP) + else: + return message.as_string().encode('ascii') def best_charset(text):