implemented email notifications

added plenty of testing and debug functions too
turned MySQLi query into a function