refactors mail functions, adds support for file attachments