diff options
-rw-r--r-- | service/pixelated/adapter/mail.py | 6 | ||||
-rw-r--r-- | service/test/unit/adapter/mail_test.py | 22 |
2 files changed, 27 insertions, 1 deletions
diff --git a/service/pixelated/adapter/mail.py b/service/pixelated/adapter/mail.py index f21ed2ef..fdfeda89 100644 --- a/service/pixelated/adapter/mail.py +++ b/service/pixelated/adapter/mail.py @@ -155,7 +155,11 @@ class InputMail(Mail): mime_multipart['Subject'] = self.headers['Subject'] mime_multipart['Date'] = self.headers['Date'] - mime_multipart.attach(MIMEText(self.body, 'plain')) + if type(self.body) is list: + for part in self.body: + mime_multipart.attach(MIMEText(part['raw'], part['content-type'])) + else: + mime_multipart.attach(MIMEText(self.body, 'plain')) return mime_multipart def to_smtp_format(self): diff --git a/service/test/unit/adapter/mail_test.py b/service/test/unit/adapter/mail_test.py index b4e19017..3bec6fc7 100644 --- a/service/test/unit/adapter/mail_test.py +++ b/service/test/unit/adapter/mail_test.py @@ -104,6 +104,19 @@ class InputMailTest(unittest.TestCase): 'tags': ['sent'] } + multipart_mail_dict = lambda x: { + 'body': [{'content-type': 'plain', 'raw': 'Hello world!'}, + {'content-type': 'html', 'raw': '<p>Hello html world!</p>'}], + 'header': { + 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], + 'to': ['to@pixelated.org', 'anotherto@pixelated.org'], + 'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'], + 'subject': 'Oi', + }, + 'ident': '', + 'tags': ['sent'] + } + def test_to_mime_multipart_should_add_blank_fields(self): pixelated.support.date.iso_now = lambda: 'date now' @@ -138,3 +151,12 @@ class InputMailTest(unittest.TestCase): smtp_format = InputMail.from_dict(self.mail_dict()).to_smtp_format() self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org") + + def test_to_mime_multipart_handles_alternative_bodies(self): + mime_multipart = InputMail.from_dict(self.multipart_mail_dict()).to_mime_multipart() + + part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!' + part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n<p>Hello html world!</p>' + + self.assertRegexpMatches(mime_multipart.as_string(), part_one) + self.assertRegexpMatches(mime_multipart.as_string(), part_two) |