Split mailing list into chunks of 500 emails to respect Postmark's API limits. Thanks Asaf for pointing this out

main
abnercoimbre 2023-09-20 12:21:07 -07:00
parent df99a03936
commit 62a11e258a
1 changed files with 31 additions and 21 deletions

View File

@ -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,7 +69,13 @@ def sendPostmarkMessage(subject, body, to):
} }
messages = [] messages = []
for email in to:
# Postmark supports 500 messages per API call.
# Split the 'to' list into chunks of 500 or fewer email addresses
for i in range(0, len(to), 500):
to_chunk = to[i:i+500]
for email in to_chunk:
message = { message = {
"From": POSTMARK_SENDER_EMAIL, "From": POSTMARK_SENDER_EMAIL,
"To": email, "To": email,
@ -93,6 +100,9 @@ def sendPostmarkMessage(subject, body, to):
result = requests.post(POSTMARK_API_URL, headers=headers, json=payload) result = requests.post(POSTMARK_API_URL, headers=headers, json=payload)
print(result.text) print(result.text)
# Clear the messages list for the next chunk
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
config = configparser.ConfigParser() config = configparser.ConfigParser()