A possible solution is to use software with a generic database plugin. Your web application does not interact with the mail server, instead it just fills a users table in a mysql database. in the simplest case this table has a username field and a password field. In most real-life setups a few additional fields are required like "user is disabled" or "user can connect with IMAP", "location of users homedir/maildir", ...
If your users can create new domains as well, you also need a table for that.
Then, you need mail server software with a database backend.
For hosting the mailboxes, you could use the dovecot IMAP/POP3 server. It supports all the fancy stuff like user quota, auto-creation of mailboxes etc.
Here, you'll find documentation on how to configure dovecot with a database backend:
http://wiki2.dovecot.org/AuthDatabase/SQL
For actually receiving the mail by stmp you also need a MTA software. Here, a good choice would be postfix. If your users can create new domains, you need to add mysql configuration that tells postfix which domains it may accept mail for.
Since I don't know your domain requirements exactly I'm just going to point you to the general postfix virtual domain hosting howto:
http://www.postfix.org/VIRTUAL_README.html
Finally, if your users should be able to send mail as well you need to configure SASL in postfix. This is quite simple if you already have dovecot configured. it boils down to telling postfix "hey, I already have user authentication configured in dovecot, I don't want to do it again, just talk to dovecot and let it do the job". Documentation is here: http://www.postfix.org/SASL_README.html#server_dovecot