mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 20:01:05 -05:00
Preserve filename of inline attachments
For ESPs that support both content-id and filename, don't drop the filename.
This commit is contained in:
@@ -111,9 +111,14 @@ class MailgunPayload(RequestsPayload):
|
|||||||
|
|
||||||
def add_attachment(self, attachment):
|
def add_attachment(self, attachment):
|
||||||
# http://docs.python-requests.org/en/v2.4.3/user/advanced/#post-multiple-multipart-encoded-files
|
# 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(
|
self.files.append(
|
||||||
(field, (attachment.name, attachment.content, attachment.mimetype))
|
(field, (name, attachment.content, attachment.mimetype))
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_metadata(self, metadata):
|
def set_metadata(self, metadata):
|
||||||
|
|||||||
@@ -118,10 +118,15 @@ class MandrillPayload(RequestsPayload):
|
|||||||
self.data["message"]["html"] = body
|
self.data["message"]["html"] = body
|
||||||
|
|
||||||
def add_attachment(self, attachment):
|
def add_attachment(self, attachment):
|
||||||
key = "images" if attachment.inline else "attachments"
|
if attachment.inline:
|
||||||
self.data["message"].setdefault(key, []).append({
|
field = "images"
|
||||||
|
name = attachment.cid
|
||||||
|
else:
|
||||||
|
field = "attachments"
|
||||||
|
name = attachment.name or ""
|
||||||
|
self.data["message"].setdefault(field, []).append({
|
||||||
"type": attachment.mimetype,
|
"type": attachment.mimetype,
|
||||||
"name": attachment.name or "",
|
"name": name,
|
||||||
"content": attachment.b64content
|
"content": attachment.b64content
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -100,11 +100,12 @@ class Attachment(object):
|
|||||||
"""A normalized EmailMessage.attachments item with additional functionality
|
"""A normalized EmailMessage.attachments item with additional functionality
|
||||||
|
|
||||||
Normalized to have these properties:
|
Normalized to have these properties:
|
||||||
name: attachment filename; may be empty string; will be Content-ID (without <>) for inline attachments
|
name: attachment filename; may be empty string
|
||||||
content
|
content: bytestream
|
||||||
mimetype: the content type; guessed if not explicit
|
mimetype: the content type; guessed if not explicit
|
||||||
inline: bool, True if attachment has a Content-ID header
|
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):
|
def __init__(self, attachment, encoding):
|
||||||
@@ -114,6 +115,7 @@ class Attachment(object):
|
|||||||
self.encoding = encoding # should we be checking attachment["Content-Encoding"] ???
|
self.encoding = encoding # should we be checking attachment["Content-Encoding"] ???
|
||||||
self.inline = False
|
self.inline = False
|
||||||
self.content_id = None
|
self.content_id = None
|
||||||
|
self.cid = ""
|
||||||
|
|
||||||
if isinstance(attachment, MIMEBase):
|
if isinstance(attachment, MIMEBase):
|
||||||
self.name = attachment.get_filename()
|
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:
|
if attachment.get_content_maintype() == "image" and attachment["Content-ID"] is not None:
|
||||||
self.inline = True
|
self.inline = True
|
||||||
self.content_id = attachment["Content-ID"] # including the <...>
|
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:
|
else:
|
||||||
(self.name, self.content, self.mimetype) = attachment
|
(self.name, self.content, self.mimetype) = attachment
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user