diff --git a/msg.go b/msg.go index a909d04..11d8ab1 100644 --- a/msg.go +++ b/msg.go @@ -476,6 +476,17 @@ func (m *Msg) SetMessageID() { m.SetMessageIDWithValue(messageID) } +// GetMessageID returns the message ID of the Msg as string value. If no message ID +// is set, an empty string will be returned +func (m *Msg) GetMessageID() string { + if msgidheader, ok := m.genHeader[HeaderMessageID]; ok { + if len(msgidheader) > 0 { + return msgidheader[0] + } + } + return "" +} + // SetMessageIDWithValue sets the message id for the mail func (m *Msg) SetMessageIDWithValue(messageID string) { m.SetGenHeader(HeaderMessageID, fmt.Sprintf("<%s>", messageID)) diff --git a/msg_test.go b/msg_test.go index 16cd196..0656ff2 100644 --- a/msg_test.go +++ b/msg_test.go @@ -805,6 +805,21 @@ func TestMsg_SetMessageIDRandomness(t *testing.T) { } } +func TestMsg_GetMessageID(t *testing.T) { + expected := "this.is.a.message.id" + msg := NewMsg() + msg.SetMessageIDWithValue(expected) + val := msg.GetMessageID() + if !strings.EqualFold(val, fmt.Sprintf("<%s>", expected)) { + t.Errorf("GetMessageID() failed. Expected: %s, got: %s", fmt.Sprintf("<%s>", expected), val) + } + msg.genHeader[HeaderMessageID] = nil + val = msg.GetMessageID() + if val != "" { + t.Errorf("GetMessageID() failed. Expected empty string, got: %s", val) + } +} + // TestMsg_FromFormat tests the FromFormat and EnvelopeFrom methods for the Msg object func TestMsg_FromFormat(t *testing.T) { tests := []struct {