From 024d554958b5bea5db220bfd84922a584d878ded Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Sun, 16 Apr 2017 20:13:44 +0100 Subject: [PATCH] fixes mailhog/MailHog#148 and mailhog/storage#7 --- message.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/message.go b/message.go index 951f19a..58e464b 100644 --- a/message.go +++ b/message.go @@ -165,24 +165,31 @@ func (m *SMTPMessage) Bytes() io.Reader { // FromBytes returns a SMTPMessage from raw message bytes (as output by SMTPMessage.Bytes()) func FromBytes(b []byte) *SMTPMessage { msg := &SMTPMessage{} + var headerDone bool for _, l := range strings.Split(string(b), "\n") { - if strings.HasPrefix(l, "HELO:<") { - l = strings.TrimPrefix(l, "HELO:<") - l = strings.TrimSuffix(l, ">\r") - msg.Helo = l - continue - } - if strings.HasPrefix(l, "FROM:<") { - l = strings.TrimPrefix(l, "FROM:<") - l = strings.TrimSuffix(l, ">\r") - msg.From = l - continue - } - if strings.HasPrefix(l, "TO:<") { - l = strings.TrimPrefix(l, "TO:<") - l = strings.TrimSuffix(l, ">\r") - msg.To = append(msg.To, l) - continue + if !headerDone { + if strings.HasPrefix(l, "HELO:<") { + l = strings.TrimPrefix(l, "HELO:<") + l = strings.TrimSuffix(l, ">\r") + msg.Helo = l + continue + } + if strings.HasPrefix(l, "FROM:<") { + l = strings.TrimPrefix(l, "FROM:<") + l = strings.TrimSuffix(l, ">\r") + msg.From = l + continue + } + if strings.HasPrefix(l, "TO:<") { + l = strings.TrimPrefix(l, "TO:<") + l = strings.TrimSuffix(l, ">\r") + msg.To = append(msg.To, l) + continue + } + if strings.TrimSpace(l) == "" { + headerDone = true + continue + } } msg.Data += l + "\n" }