Question

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?

Was it helpful?

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.

OTHER TIPS

try sqlite. It has default python bindings in the standard library and should work for a useful level of load (or so I am told)

You could, as suggested, use sqlite for this. The main question is: How mans is "a lot of submissions"? I it is below of a few per second this would work. Otherwise the database file will be locked all the time and you have another problem.

But you could it also keep it plain, stupid and simple: write files to disk. For every sumission a file is written in a temporary directory (if the mail server is down). Then you add a few lines to the mailserver startup script that reads the directory and sends out mails. No databases, no locking problems and if you use a directory for which a quota is set (or a ramdisk with fixed size) you shouldn't run into any problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top