Skip to content
Browse files

Moved message encoding to repoze.sendmail.

  • Loading branch information...
1 parent 5f6b3e5 commit 49d86a25cb0ca960b1ffee2190c25be786ef1823 @rpatterson rpatterson committed Mar 15, 2012
Showing with 9 additions and 64 deletions.
  1. +9 −38 pyramid_mailer/response.py
  2. +0 −26 pyramid_mailer/tests.py
View
47 pyramid_mailer/response.py
@@ -36,19 +36,14 @@
# BBB Python 2 vs 3 compat
from __future__ import unicode_literals
-import sys
import os
+import sys
import mimetypes
import string
-from email import encoders
-from email.charset import Charset
-from email import header
-from email.utils import parseaddr
from email.mime.base import MIMEBase
-ADDRESS_HEADERS_WHITELIST = ['From', 'To', 'Delivered-To', 'Cc']
-DEFAULT_ENCODING = "utf-8"
-VALUE_IS_EMAIL_ADDRESS = lambda v: '@' in v
+from repoze.sendmail import encoding
+
def normalize_header(header):
return string.capwords(header.lower(), '-')
@@ -344,10 +339,11 @@ def to_message(mail):
(ctype, params, exc.message))
for k in mail.keys():
- if k in ADDRESS_HEADERS_WHITELIST:
- out[k] = header_to_mime_encoding(mail[k])
- else:
- out[k] = header_to_mime_encoding(mail[k])
+ value = mail[k]
+ if k.lower() in encoding.ADDR_HEADERS:
+ if is_nonstr_iter(value): # not a string
+ value = ", ".join(value)
+ out[k] = value
out.extract_payload(mail)
@@ -384,14 +380,7 @@ def extract_payload(self, mail):
# replicate the content-disposition settings
self.add_header('Content-Disposition', cdisp, **cdisp_params)
- charset = None
- body = mail.body
- if hasattr(mail.body, 'encode'):
- charset = best_charset(mail.body)
- if charset:
- body = body.encode(charset)
- self.set_payload(body, charset=charset)
- encoders.encode_base64(self)
+ self.set_payload(mail.body)
def __repr__(self):
return "<MIMEPart '%s/%s': '%s', %r, multipart=%r>" % (
@@ -402,16 +391,6 @@ def __repr__(self):
self.is_multipart())
-def best_charset(text):
- for charset in 'ascii', 'latin_1', 'utf_8':
- try:
- text.encode(charset)
- except UnicodeError:
- pass
- else:
- return charset
-
-
def is_nonstr_iter(v):
if isinstance(v, str):
return False
@@ -421,11 +400,3 @@ def is_nonstr_iter(v):
if sys.version < '3':
def is_nonstr_iter(v):
return hasattr(v, '__iter__')
-
-
-def header_to_mime_encoding(value, charset=DEFAULT_ENCODING):
- if is_nonstr_iter(value): # not a string
- value = ", ".join(value)
- if value:
- charset = best_charset(value)
- return header.Header(value, charset=charset)
View
26 pyramid_mailer/tests.py
@@ -921,39 +921,13 @@ def _makeOne(self, type, **params):
from pyramid_mailer.response import MIMEPart
return MIMEPart(type, **params)
- def test_extract_payload(self):
- mail = DummyPart()
- mail.content_encoding['Content-Type'] = ('application/json', {})
- part = self._makeOne('application/json')
- part.extract_payload(mail)
- self.assert_(part.get_payload().startswith('Ym9keQ=='))
-
def test___repr__(self):
part = self._makeOne('text/html')
result = repr(part)
self.assertEqual(
result,
"<MIMEPart 'html/text': 'text/html', None, multipart=False>")
-class Test_header_to_mime_encoding(unittest.TestCase):
- def _callFUT(self, value, not_email=False):
- from pyramid_mailer.response import header_to_mime_encoding
- return header_to_mime_encoding(value)
-
- def test_empty_value(self):
- result = self._callFUT('')
- self.assertEqual(result, '')
-
- def test_list_value(self):
- L = ['chrism@plope.com', 'billg@microsoft.com']
- result = self._callFUT(L)
- self.assertEqual(str(result), 'chrism@plope.com, billg@microsoft.com')
-
- def test_nonempty_nonlist_value(self):
- val = 'chrism@plope.com'
- result = self._callFUT(val)
- self.assertEqual(str(result), 'chrism@plope.com')
-
class Dummy(object):
pass

0 comments on commit 49d86a2

Please sign in to comment.
Something went wrong with that request. Please try again.