To store mail message in database has better advantage rather than storing in filesystem. Using database then you can 'link' the mail and do wonders with it. The implementation is cleaner. Especially when it comes to backup, using database you just need to backup the database. It's also scalable as the storage are in DB, the DB server can actually sits on a different machine.
Scripts similar to what you need : SocketMail , it is using MySQL.
Check it out.