diff --git a/anymail/backends/mailgun.py b/anymail/backends/mailgun.py index f65361d..0964761 100644 --- a/anymail/backends/mailgun.py +++ b/anymail/backends/mailgun.py @@ -111,9 +111,14 @@ class MailgunPayload(RequestsPayload): def add_attachment(self, attachment): # http://docs.python-requests.org/en/v2.4.3/user/advanced/#post-multiple-multipart-encoded-files - field = "inline" if attachment.inline else "attachment" + if attachment.inline: + field = "inline" + name = attachment.cid + else: + field = "attachment" + name = attachment.name self.files.append( - (field, (attachment.name, attachment.content, attachment.mimetype)) + (field, (name, attachment.content, attachment.mimetype)) ) def set_metadata(self, metadata): diff --git a/anymail/backends/mandrill.py b/anymail/backends/mandrill.py index 8431873..710bdc9 100644 --- a/anymail/backends/mandrill.py +++ b/anymail/backends/mandrill.py @@ -118,10 +118,15 @@ class MandrillPayload(RequestsPayload): self.data["message"]["html"] = body def add_attachment(self, attachment): - key = "images" if attachment.inline else "attachments" - self.data["message"].setdefault(key, []).append({ + if attachment.inline: + field = "images" + name = attachment.cid + else: + field = "attachments" + name = attachment.name or "" + self.data["message"].setdefault(field, []).append({ "type": attachment.mimetype, - "name": attachment.name or "", + "name": name, "content": attachment.b64content }) diff --git a/anymail/utils.py b/anymail/utils.py index 00fd795..53db4bb 100644 --- a/anymail/utils.py +++ b/anymail/utils.py @@ -100,11 +100,12 @@ class Attachment(object): """A normalized EmailMessage.attachments item with additional functionality Normalized to have these properties: - name: attachment filename; may be empty string; will be Content-ID (without <>) for inline attachments - content + name: attachment filename; may be empty string + content: bytestream mimetype: the content type; guessed if not explicit inline: bool, True if attachment has a Content-ID header - content_id: for inline, the Content-ID (with <>) + content_id: for inline, the Content-ID (*with* <>) + cid: for inline, the Content-ID *without* <> """ def __init__(self, attachment, encoding): @@ -114,6 +115,7 @@ class Attachment(object): self.encoding = encoding # should we be checking attachment["Content-Encoding"] ??? self.inline = False self.content_id = None + self.cid = "" if isinstance(attachment, MIMEBase): self.name = attachment.get_filename() @@ -123,7 +125,7 @@ class Attachment(object): if attachment.get_content_maintype() == "image" and attachment["Content-ID"] is not None: self.inline = True self.content_id = attachment["Content-ID"] # including the <...> - self.name = self.content_id[1:-1] # without the <, > + self.cid = self.content_id[1:-1] # without the <, > else: (self.name, self.content, self.mimetype) = attachment