Fix email code & tests
Merge pull request #2478 from riking/patch-email

Fix email code & tests