Add GetMessageID method to Msg

Introduced GetMessageID to retrieve the Message ID from the Msg
This commit is contained in:
Winni Neessen 2024-09-19 11:46:53 +02:00
parent 3bdb6f7cca
commit 69e211682c
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 26 additions and 0 deletions

11
msg.go
View file

@ -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))

View file

@ -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 {