Overview
Transactional email includes password resets, account notices, receipts and contact-form messages. Reliable delivery requires more than calling mail(): applications need authenticated transport, clear error handling and domain-level email configuration.
Use an SMTP library
Use a maintained mail library such as PHPMailer or Symfony Mailer instead of manually assembling MIME messages. Configure SMTP credentials outside source control and use TLS with certificate verification. Do not disable peer verification to make a connection succeed.
Separate message intent from transport
Keep the application event (for example, “password reset requested”) separate from the SMTP implementation. A mail service can build the message and delegate delivery to a transport. This makes it easier to test message content without sending real mail.
Handle failure deliberately
Log delivery failures without storing passwords, reset tokens or message contents containing sensitive data. For important notifications, consider a database-backed outbox and retry worker so a temporary SMTP outage does not lose the message. Make retries bounded and avoid duplicate sends by using an idempotent message identifier.
Improve deliverability
Publish SPF and DKIM records and configure DMARC for the sending domain. Use a consistent From address on a domain you control, a valid Reply-To where appropriate and meaningful plain-text alternatives. Monitor bounces and complaints.
Official references
PHPMailer documentation Symfony Mailer documentation
Use authenticated SMTP through a maintained mail library
For application email, prefer a maintained library and authenticated SMTP rather than building MIME messages manually or relying on an unrestricted mail() call. Keep SMTP credentials in private configuration and use TLS according to the provider's documented settings.
Separate message creation from delivery
Create a message with a clear sender identity, recipient, subject and plain-text alternative. Validate recipient addresses and avoid accepting arbitrary headers from form input. A contact form should send to a fixed site mailbox; the visitor's address belongs in a validated Reply-To field, not as the trusted From address.
Handle transient failures
Record a delivery attempt with a message identifier and safe diagnostic context. For important notifications, use a queue or an outbox table so a temporary SMTP outage does not lose the business event. Retry transient failures with bounded exponential backoff; do not retry permanent address or policy failures indefinitely.
Protect deliverability
Configure SPF, DKIM and DMARC for the sending domain. Monitor bounces and complaints, keep transactional mail separate from bulk marketing and provide a valid unsubscribe process where legally required. Do not log message bodies containing password-reset tokens or sensitive personal data.
Test before production
Use a sandbox mailbox during development. Test provider authentication failures, timeouts, invalid recipients and duplicate submissions. Ensure the user sees a clear result without exposing SMTP credentials or internal error details.