-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsend_batch.py
More file actions
42 lines (32 loc) · 1.09 KB
/
Copy pathsend_batch.py
File metadata and controls
42 lines (32 loc) · 1.09 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
"""
Send several distinct messages in one call. django.core.mail.send_mass_mail
takes tuples of (subject, message, from_email, recipient_list) and sends them
through a single connection — the Django backend batches them into Postmark's
send_batch API (up to 500 per request) rather than one request per message.
Run:
poetry run python examples/django/send_batch.py
python examples/django/send_batch.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 send_mass_mail # noqa: E402
SENDER = os.environ["POSTMARK_SENDER_EMAIL"]
sent_count = send_mass_mail(
(
("Batch 1", "Hello Receiver 1", SENDER, ["receiver1@example.com"]),
("Batch 2", "Hello Receiver 2", SENDER, ["receiver2@example.com"]),
)
)
print(f"Sent: {sent_count}")