-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsend_with_external_image.py
More file actions
62 lines (49 loc) · 1.92 KB
/
Copy pathsend_with_external_image.py
File metadata and controls
62 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Send HTML email referencing an external image URL (e.g. a tracking pixel or a
logo hosted on your own server) — loaded by the recipient's email client at
open time, no attachment needed.
Note: inline (Content-ID / cid:) images are NOT supported through this Django
backend, since Django only exposes that via passing a raw email.mime.base.MIMEBase
object to EmailMessage.attach() — a legacy path this backend intentionally
doesn't support (see the Django-Backend wiki page). For inline images, either
host the image externally as shown here, or send directly with
postmark.ServerClient / postmark.sync.ServerClient using an Attachment with
content_id set (see examples/async/outbound_messages/send_with_inline_and_external_images.py).
Run:
poetry run python examples/django/send_with_external_image.py
python examples/django/send_with_external_image.py # with venv active
"""
import os
import django
from django.conf import settings
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
if not settings.configured:
settings.configure(
EMAIL_BACKEND="postmark.django.EmailBackend",
POSTMARK_SERVER_TOKEN=os.environ["POSTMARK_SERVER_TOKEN"],
)
django.setup()
from django.core.mail import EmailMultiAlternatives # noqa: E402
SENDER = os.environ["POSTMARK_SENDER_EMAIL"]
# Fetched by the recipient's email client at open time, so the server can
# record the open event.
TRACKING_PIXEL_URL = "https://track.example.com/pixel.png"
html_body = f"""
<html><body>
<p>Hello! Thanks for reading.</p>
<img src="{TRACKING_PIXEL_URL}" width="1" height="1" alt="" />
</body></html>
"""
message = EmailMultiAlternatives(
subject="Hello — with tracking image",
body="Hello! Thanks for reading. (Open the HTML version to see the image.)",
from_email=SENDER,
to=["receiver@example.com"],
)
message.attach_alternative(html_body, "text/html")
message.send()
print("Sent.")