Search code examples
pythonemailrace-conditiondata-formats

Ensuring contact form email isn't lost (python)


I have a website with a contact form. User submits name, email and message and the site emails me the details.

Very occasionally my server has a problem with it's email system and so the user gets an error and those contact details are lost. (Don't say: get a better server, any server can have email go down now and then and we do get a lot of submissions).

I would like to implement a system that could store the user's details if the mail sending function returns with an error code. Then on every further submission, check for any stored submissions and try to send them off to me.

But how to store the data?

I'm using python so I thought about using shelve (single file semi-database). Or maybe someone could suggest a better data format? (I do think a full database solution would be overkill.)

The problem I see with a single file approach is race-conditions: two or more failed emails at the same time would cause two edits to the data file resulting in data corruption.

So what to do? Multi-file solution, file locking or something else?


Solution

  • When we implement email sending functionality in our environment we do it in a decoupled way. So for example a user would submit their data which would get stored in a database. We then have a separate service that runs, queries the database and sends out email. That way if there are ever any email server issues, the service will just try again later, data and user confidence is never lost.