Skip to content

Commit

Permalink
Strips brackets from mime attachment's content-id (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
sklarsa authored Apr 26, 2018
1 parent f91c3fb commit c9aae81
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sendgrid_backend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def _build_sg_mail(self, msg):
attachment.type = attch.get_content_type()
content_id = attch.get("Content-ID")
if content_id:
# Strip brackets since sendgrid's api adds them
if content_id.startswith("<") and content_id.endswith(">"):
content_id = content_id[1:-1]
attachment.content_id = content_id
attachment.disposition = "inline"

Expand Down
4 changes: 3 additions & 1 deletion test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,16 @@ def test_mime(self):
msg.attach_alternative(content, "text/html")
with open("test/linux-penguin.png", "rb") as f:
img = MIMEImage(f.read())
img.add_header("Content-ID", "linux_penguin")
img.add_header("Content-ID", "<linux_penguin>")
msg.attach(img)

result = self.backend._build_sg_mail(msg)
self.assertEqual(len(result["content"]), 2)
self.assertDictEqual(result["content"][0], {"type": "text/plain", "value": " "})
self.assertDictEqual(result["content"][1], {"type": "text/html", "value": content})
self.assertEqual(len(result["attachments"]), 1)
self.assertEqual(result["attachments"][0]["content_id"], "linux_penguin")

with open("test/linux-penguin.png", "rb") as f:
if sys.version_info >= (3.0, 0.0, ):
self.assertEqual(bytearray(result["attachments"][0]["content"], "utf-8"), base64.b64encode(f.read()))
Expand Down

0 comments on commit c9aae81

Please sign in to comment.