Do not send critical email inside a fragile request
A user request should not fail merely because an SMTP server is temporarily unavailable after the business action has already succeeded. For important notifications, persist an email job and let a worker deliver it.
Separate message intent from transport
Create a notification service that accepts a business event and produces a message intent. Keep templates, recipient selection and SMTP transport separate. This makes it easier to test content without sending real email.
Track delivery state
Store a message ID, recipient reference, template version, status, attempt count and timestamps. Avoid storing unnecessary message content or sensitive data. Record provider responses safely, redacting credentials and personal information where appropriate.
Retry carefully
Retry transient network and provider errors with backoff. Do not retry invalid recipient addresses indefinitely. Use an idempotency key or unique notification event key to avoid duplicate sends when a worker crashes after SMTP acceptance but before recording success.
Monitor deliverability
Configure SPF, DKIM and DMARC for the sending domain. Monitor bounces, complaints and provider reputation. Keep unsubscribe and consent rules appropriate to the type of email and applicable requirements.
Checklist
- Queue important messages outside the request.
- Separate templates from transport.
- Track attempts and provider outcomes.
- Retry transient errors with backoff.
- Prevent duplicate notifications.
- Configure SPF, DKIM and DMARC.
- Monitor bounces and complaints.
References
PHPMailer Documentation · OWASP Email Validation and Verification