summaryrefslogtreecommitdiff
path: root/docs/topics
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2025-12-03 22:50:06 +0000
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-08 09:44:19 -0500
commitaf60ae48d98888826928b1c2def47a980f9a4af4 (patch)
treeaf4216adff13e45e5a05dbed7112ed04f0cf2e6a /docs/topics
parente726254a380f2a35a2fcf71143e96cb5987d8102 (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/topics')
-rw-r--r--docs/topics/email.txt15
1 files changed, 12 insertions, 3 deletions
diff --git a/docs/topics/email.txt b/docs/topics/email.txt
index 0ce5d8f767..bad4a79d61 100644
--- a/docs/topics/email.txt
+++ b/docs/topics/email.txt
@@ -471,17 +471,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()``.