Rebase PR-337 Emails output Fixes: LOGSTASH-827 and LOGSTASH-841

Rebased work from https://github.com/logstash/logstash/pull/337

Please note this PR also contains the exact same fix as in https://github.com/logstash/logstash/pull/355