Add tests and fix what they revealed

* A little refactoring to make it easier to test
* New setting for actual email backend