← Back to articles

Django · Reliability

Persist the enquiry before sending email

A contact form should keep a visitor’s message even when the mail server fails.

An enquiry envelope stored in a database vault before following an outbound email delivery path.
Store the enquiry reliably before attempting email delivery.

The portfolio contact form validates a Django ModelForm, saves a Contact record, then attempts an email notification. Persistence comes first because email availability should not decide whether an enquiry is accepted.

The notification function sets a delivery status of sent or failed. A failure leaves the enquiry available in the admin inbox, where a staff member can retry failed notifications. The email uses a configured sender and puts the visitor address in Reply-To instead of impersonating that address.

The form also includes a honeypot and an IP-based submission limit. Both the original home POST and the dedicated contact route reuse the same form and request context. Neither field validation nor delivery failures should erase an accepted message.

For this small application the email attempt has a short timeout and happens synchronously after saving. A larger service could use a durable queue to reduce request latency, with idempotency and delivery observation tested separately. Tests cover successful delivery, mail-server failure and saved-message state.