Consolidated a lot of the email-handling code.

  This updates #131 - still need to move the attachment handling
 to something more sane, and probably reassess the hook-calling code.