Improved encoding of emails

Closes #1566