-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsend_simple_with_attachment.py
More file actions
49 lines (36 loc) · 1.19 KB
/
Copy pathsend_simple_with_attachment.py
File metadata and controls
49 lines (36 loc) · 1.19 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
"""
Send an email with an attachment through Postmark's Django backend.
Attachment content is base64-encoded automatically — pass the raw
bytes/str you'd normally give EmailMessage.attach(), same as any other
Django email backend.
Run:
poetry run python examples/django/send_simple_with_attachment.py
python examples/django/send_simple_with_attachment.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 EmailMessage # noqa: E402
SENDER = os.environ["POSTMARK_SENDER_EMAIL"]
message = EmailMessage(
subject="Your report and resources",
body="Please find your report attached.",
from_email=SENDER,
to=["receiver@example.com"],
)
message.attach("report.txt", "Q3 sales are up 12%.", "text/plain")
with open("/path/to/book.pdf", "rb") as f:
message.attach("book.pdf", f.read(), "application/pdf")
message.send()
print("Sent.")