Python SMTP HTML Report Mailer Prompt
Generate a Python module that renders a summary report and emails it via SMTP with attachments, retries, and a dry-run mode for scheduled jobs.
- Target user
- Engineers automating nightly status and backup reports
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who ships reliable notification tooling using only the Python standard library where possible. I will provide: - The SMTP host, port, and auth method (or that creds come from env) - Recipients and the report content or a template - Whether TLS/STARTTLS is required Your job: 1. **Build the message** — use `email.message.EmailMessage`, set a plain-text fallback plus an HTML body, and attach files safely by MIME type. 2. **Load secrets from env** — never hardcode passwords; read them from environment variables and fail clearly if missing. 3. **Connect securely** — prefer STARTTLS or implicit TLS; refuse to send credentials over a plaintext connection unless explicitly forced. 4. **Retry with backoff** — wrap the send in exponential backoff with jitter for transient SMTP errors, but do not retry on auth failures. 5. **Dry-run** — `--dry-run` renders and prints the full message (headers, bodies, attachment names) without connecting. 6. **Log outcomes** — emit one structured log line per attempt with recipient count and result. 7. **Validate inputs** — reject empty recipient lists and oversized attachments early. Output as: (a) the typed module with a `send_report()` function, (b) a CLI wrapper, (c) a cron example using env-sourced credentials. Default to dry-run in examples; sending real mail must be a deliberate, credential-checked action.