Commit d831f227 authored by Maurits van Rees's avatar Maurits van Rees

Products.MailHost: merged r107689 from branches/2.12:

Fixed possible TypeError while sending multipart emails.
(Zope 2.11 and 2.10 are not affected.)
parent 03dd2820
...@@ -417,16 +417,31 @@ def _mungeHeaders(messageText, mto=None, mfrom=None, subject=None, ...@@ -417,16 +417,31 @@ def _mungeHeaders(messageText, mto=None, mfrom=None, subject=None,
# we don't use get_content_type because that has a default # we don't use get_content_type because that has a default
# value of 'text/plain' # value of 'text/plain'
mo.set_type(msg_type) mo.set_type(msg_type)
charset_match = CHARSET_RE.search(mo['Content-Type'] or '') if not mo.is_multipart():
if charset and not charset_match: charset_match = CHARSET_RE.search(mo['Content-Type'] or '')
# Don't change the charset if already set if charset and not charset_match:
# This encodes the payload automatically based on the default # Don't change the charset if already set
# encoding for the charset # This encodes the payload automatically based on the default
mo.set_charset(charset) # encoding for the charset
elif charset_match and not charset: mo.set_charset(charset)
# If a charset parameter was provided use it for header encoding below, elif charset_match and not charset:
# Otherwise, try to use the charset provided in the message. # If a charset parameter was provided use it for header encoding below,
charset = charset_match.groups()[0] # Otherwise, try to use the charset provided in the message.
charset = charset_match.groups()[0]
else:
# Do basically the same for each payload as for the complete
# multipart message.
for index, payload in enumerate(mo.get_payload()):
if not isinstance(payload, Message):
payload = message_from_string(payload)
charset_match = CHARSET_RE.search(payload['Content-Type'] or '')
if payload.get_filename() is None:
# No binary file
if charset and not charset_match:
payload.set_charset(charset)
elif charset_match and not charset:
charset = charset_match.groups()[0]
mo.get_payload()[index] = payload
# Parameters given will *always* override headers in the messageText. # Parameters given will *always* override headers in the messageText.
# This is so that you can't override or add to subscribers by adding # This is so that you can't override or add to subscribers by adding
......
...@@ -518,6 +518,92 @@ A M\xc3\xa9ssage""") ...@@ -518,6 +518,92 @@ A M\xc3\xa9ssage""")
statusTemplate='check_status') statusTemplate='check_status')
self.failUnlessEqual(result, 'Message Sent') self.failUnlessEqual(result, 'Message Sent')
def testSendMultiPartAlternativeMessage(self):
msg = ("""\
Content-Type: multipart/alternative; boundary="===============0490954888=="
MIME-Version: 1.0
Date: Sun, 27 Aug 2006 17:00:00 +0200
Subject: My multipart email
To: Foo Bar <foo@domain.com>
From: sender@domain.com
--===============0490954888==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
This is plain text.
--===============0490954888==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
<p>This is html.</p>
--===============0490954888==--
""")
mailhost = self._makeOne('MailHost')
# Specifying a charset for the header may have unwanted side
# effects in the case of multipart mails.
# (TypeError: expected string or buffer)
mailhost.send(msg, charset='utf-8')
self.assertEqual(mailhost.sent, msg)
def testSendMultiPartMixedMessage(self):
msg = ("""\
Content-Type: multipart/mixed; boundary="XOIedfhf+7KOe/yw"
Content-Disposition: inline
MIME-Version: 1.0
Date: Sun, 27 Aug 2006 17:00:00 +0200
Subject: My multipart email
To: Foo Bar <foo@domain.com>
From: sender@domain.com
--XOIedfhf+7KOe/yw
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
This is a test with as attachment OFS/www/new.gif.
--XOIedfhf+7KOe/yw
Content-Type: image/gif
Content-Disposition: attachment; filename="new.gif"
Content-Transfer-Encoding: base64
R0lGODlhCwAQAPcAAP8A/wAAAFBQUICAgMDAwP8AAIAAQAAAoABAgIAAgEAAQP//AP//gACA
gECAgP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAALABAAAAg7AAEIFKhgoEGC
CwoeRKhwoYKEBhVIfLgg4UQAFCtqbJixYkOEHg9SHDmQJEmMEBkS/IiR5cKXMGPKDAgAOw==
--XOIedfhf+7KOe/yw
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: attachment; filename="test.txt"
Content-Transfer-Encoding: quoted-printable
D=EDt =EFs =E9=E9n test
--XOIedfhf+7KOe/yw--
""")
mailhost = self._makeOne('MailHost')
# Specifying a charset for the header may have unwanted side
# effects in the case of multipart mails.
# (TypeError: expected string or buffer)
mailhost.send(msg, charset='utf-8')
self.assertEqual(mailhost.sent, msg)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment