Incremental refactor of failmail main().

It was getting really long, and hard to test. I've done the shortest
pass at splitting it up into some pretty ugly functions, and the next
pass should make it a little better still.