Take a look at https://bitbucket.tornevall.net/users/tornevall/repos/checkmail/browse
This tiny script "suite" takes a look at internal mailservers - if they anwer or not - and if they stop working due to human errors that does not include configuration errors, this script will also restart the services that went down.
The script is based on PHP - initially it was written as a bash script that was running through common commands to test the mail. But some of the tests lies outside (as I know it) the bashifyable world so the tests are implemented as a PHP service instead. It runs through following protocols, which courier supports:
- imap with tls.
- pop3 with tls.
- smtp (not testing tls since if smtp is DOWN the tls parts are also down).