ferrero-opentext/Python-Version/test_mailgun_recipients.py
nickviljoen e6a6357403 Update Mailgun test: try US/EU endpoints, handle non-JSON errors
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 13:29:27 +02:00

88 lines
3 KiB
Python

#!/usr/bin/env python3
"""
Quick test: Send via Mailgun API with multiple recipients
to diagnose daily report delivery issue.
"""
import os
import sys
import requests
# Load from environment (same as production)
api_key = os.environ.get('MAILGUN_API_KEY')
domain = os.environ.get('MAILGUN_DOMAIN')
sender = os.environ.get('MAILGUN_SENDER_EMAIL') or os.environ.get('SENDER_EMAIL')
if not api_key or not domain:
print("ERROR: MAILGUN_API_KEY and MAILGUN_DOMAIN must be set")
sys.exit(1)
print("Using domain: {}".format(domain))
print("Using sender: {}".format(sender))
print("API key: {}...{}".format(api_key[:8], api_key[-8:]))
print()
# Try both US and EU endpoints
endpoints = [
("US", "https://api.mailgun.net/v3/{}/messages".format(domain)),
("EU", "https://api.eu.mailgun.net/v3/{}/messages".format(domain)),
]
# First, find which endpoint works
working_url = None
for region, url in endpoints:
print("Testing {} endpoint: {}".format(region, url))
test_data = {
"from": sender,
"to": ["nick.viljoen@oliver.agency"],
"subject": "Mailgun Endpoint Test - {} Region".format(region),
"html": "<p>Testing {} endpoint</p>".format(region),
}
resp = requests.post(url, auth=("api", api_key), data=test_data)
print(" Status: {}".format(resp.status_code))
print(" Response: {}".format(resp.text[:500]))
if resp.status_code == 200:
working_url = url
print(" >>> {} endpoint works!".format(region))
break
print()
if not working_url:
print("\nERROR: Neither US nor EU endpoint accepted the API key.")
print("Check that MAILGUN_API_KEY is correct and the domain is verified.")
sys.exit(1)
print()
print("=" * 60)
print("Using working endpoint: {}".format(working_url))
print("=" * 60)
# --- Test 1: Comma-separated string in list (how daily report currently sends) ---
print()
print("TEST 1: Comma-separated string in list (current daily report format)")
data1 = {
"from": sender,
"to": ["nick.viljoen@oliver.agency,daveporter@oliver.agency"],
"subject": "Mailgun Test 1 - Comma-Separated in List",
"html": "<h2>Test 1</h2><p>Comma-separated string in list. If you see this, the current format works.</p>",
}
resp1 = requests.post(working_url, auth=("api", api_key), data=data1)
print(" Status: {}".format(resp1.status_code))
print(" Response: {}".format(resp1.text[:500]))
# --- Test 2: Multiple recipients as separate list items (proper format) ---
print()
print("TEST 2: Separate list items (proper format)")
data2 = {
"from": sender,
"to": ["nick.viljoen@oliver.agency", "daveporter@oliver.agency"],
"subject": "Mailgun Test 2 - Separate List Items",
"html": "<h2>Test 2</h2><p>Separate list items. If you see this, the split format works.</p>",
}
resp2 = requests.post(working_url, auth=("api", api_key), data=data2)
print(" Status: {}".format(resp2.status_code))
print(" Response: {}".format(resp2.text[:500]))
print()
print("=" * 60)
print("DONE - Check inboxes for both tests")
print("=" * 60)