Split mailing list into chunks of 500 emails to respect Postmark's API limits. Thanks Asaf for pointing this out
parent
df99a03936
commit
62a11e258a
50
send_mail.py
50
send_mail.py
|
@ -5,6 +5,7 @@
|
||||||
# ============================ Contributors =========================
|
# ============================ Contributors =========================
|
||||||
# Bug & warning fixes
|
# Bug & warning fixes
|
||||||
# Jacob Bell (@MysteriousJ)
|
# Jacob Bell (@MysteriousJ)
|
||||||
|
# Asaf Gartner
|
||||||
#
|
#
|
||||||
# Emotional Support
|
# Emotional Support
|
||||||
# Cucui Ganon Rosario
|
# Cucui Ganon Rosario
|
||||||
|
@ -68,30 +69,39 @@ def sendPostmarkMessage(subject, body, to):
|
||||||
}
|
}
|
||||||
|
|
||||||
messages = []
|
messages = []
|
||||||
for email in to:
|
|
||||||
message = {
|
# Postmark supports 500 messages per API call.
|
||||||
"From": POSTMARK_SENDER_EMAIL,
|
# Split the 'to' list into chunks of 500 or fewer email addresses
|
||||||
"To": email,
|
for i in range(0, len(to), 500):
|
||||||
"TemplateId": POSTMARK_TEMPLATE_ID,
|
to_chunk = to[i:i+500]
|
||||||
"TemplateModel": {
|
|
||||||
"name": SENDER_NAME,
|
for email in to_chunk:
|
||||||
"email": POSTMARK_SENDER_EMAIL,
|
message = {
|
||||||
"body": body,
|
"From": POSTMARK_SENDER_EMAIL,
|
||||||
"subject": subject
|
"To": email,
|
||||||
},
|
"TemplateId": POSTMARK_TEMPLATE_ID,
|
||||||
"MessageStream": POSTMARK_MESSAGE_STREAM
|
"TemplateModel": {
|
||||||
|
"name": SENDER_NAME,
|
||||||
|
"email": POSTMARK_SENDER_EMAIL,
|
||||||
|
"body": body,
|
||||||
|
"subject": subject
|
||||||
|
},
|
||||||
|
"MessageStream": POSTMARK_MESSAGE_STREAM
|
||||||
|
}
|
||||||
|
messages.append(message)
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"Messages": messages
|
||||||
}
|
}
|
||||||
messages.append(message)
|
|
||||||
|
|
||||||
payload = {
|
# Debug:
|
||||||
"Messages": messages
|
# print(json.dumps(payload, indent=4))
|
||||||
}
|
|
||||||
|
|
||||||
# Debug:
|
result = requests.post(POSTMARK_API_URL, headers=headers, json=payload)
|
||||||
# print(json.dumps(payload, indent=4))
|
print(result.text)
|
||||||
|
|
||||||
result = requests.post(POSTMARK_API_URL, headers=headers, json=payload)
|
# Clear the messages list for the next chunk
|
||||||
print(result.text)
|
messages.clear()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Create a ConfigParser object and read the INI file
|
# Create a ConfigParser object and read the INI file
|
||||||
|
|
Loading…
Reference in New Issue