Normalize text/html alternative handling in BasePayload

This commit is contained in:
medmunds
2016-03-05 15:16:49 -08:00
parent a6c0eb5974
commit 518e6e86f8
2 changed files with 11 additions and 11 deletions

View File

@@ -322,11 +322,16 @@ class BasePayload(object):
def set_alternatives(self, alternatives):
for content, mimetype in alternatives:
self.add_alternative(content, mimetype)
if mimetype == "text/html":
# This assumes that there's at most one html alternative,
# and so it should be the html body. (Most ESPs don't
# support multiple html alternative parts anyway.)
self.set_html_body(content)
else:
self.add_alternative(content, mimetype)
def add_alternative(self, content, mimetype):
raise NotImplementedError("%s.%s must implement add_alternative or set_alternatives" %
(self.__class__.__module__, self.__class__.__name__))
self.unsupported_feature("alternative part with type '%s'" % mimetype)
def set_attachments(self, attachments):
for attachment in attachments:

View File

@@ -117,15 +117,10 @@ class MandrillPayload(RequestsPayload):
self.data["message"]["text"] = body
def set_html_body(self, body):
self.data["message"]["html"] = body
def add_alternative(self, content, mimetype):
if mimetype != 'text/html':
self.unsupported_feature("alternative part with mimetype '%s'" % mimetype)
elif "html" in self.data["message"]:
if "html" in self.data["message"]:
# second html body could show up through multiple alternatives, or html body + alternative
self.unsupported_feature("multiple html parts")
else:
self.set_html_body(content)
self.data["message"]["html"] = body
def add_attachment(self, attachment):
key = "images" if attachment.inline else "attachments"