diff options
| author | Adam Johnson <me@adamj.eu> | 2025-12-03 22:50:06 +0000 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-08 09:44:51 -0500 |
| commit | 9f61563b4239a55783ed8ee2877f0e9d4b4a35bf (patch) | |
| tree | d05747689585f18c1465559c15ab238009b3c1c2 /docs | |
| parent | 2e7133c58b24ebd32dc2030cc0665ce545c2670c (diff) | |
Refs #35581 -- Fixed email image inline attachment example.
1. Added imports and setup for clarity.
2. Removed adding `<` and `>` to Content-ID, as `make_msgid()` already
includes them.
3. Removed `$` from reference in HTML, and instead stripped `<>` there, as
required by HTML `cid:` references.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/email.txt | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/docs/topics/email.txt b/docs/topics/email.txt index c29eae5d74..d8e8f177d5 100644 --- a/docs/topics/email.txt +++ b/docs/topics/email.txt @@ -476,17 +476,26 @@ email backend API :ref:`provides an alternative directly to the resulting message. For example, to attach an inline image with a :mailheader:`Content-ID`:: + import email.utils + from email.message import MIMEPart + from django.core.mail import EmailMultiAlternatives + + message = EmailMultiAlternatives(...) + image_data_bytes = ... # Load image as bytes + + # Create a random Content-ID, including angle brackets cid = email.utils.make_msgid() inline_image = email.message.MIMEPart() inline_image.set_content( image_data_bytes, maintype="image", - subtype="png", + subtype="png", # or "jpeg", etc. depending on the image type disposition="inline", - cid=f"<{cid}>", + cid=cid, ) message.attach(inline_image) - message.attach_alternative(f'… <img src="cid:${cid}"> …', "text/html") + # Refer to Content-ID in HTML without angle brackets + message.attach_alternative(f'… <img src="cid:{cid[1:-1]}"> …', "text/html") Python's :meth:`email.contentmanager.set_content` documentation describes the supported arguments for ``MIMEPart.set_content()``. |
