mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 15:32:54 +01:00
#147 add remove attachments and embeds methods
This commit is contained in:
parent
0bd5390e37
commit
3c2c106cb4
2 changed files with 133 additions and 1 deletions
18
msg.go
18
msg.go
|
@ -638,16 +638,32 @@ func (m *Msg) SetAttachements(ff []*File) {
|
||||||
m.attachments = ff
|
m.attachments = ff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnsetAllAttachments unset the attachments of the message.
|
||||||
|
func (m *Msg) UnsetAllAttachments() {
|
||||||
|
m.attachments = nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetEmbeds returns the embeds of the Msg
|
// GetEmbeds returns the embeds of the Msg
|
||||||
func (m *Msg) GetEmbeds() []*File {
|
func (m *Msg) GetEmbeds() []*File {
|
||||||
return m.embeds
|
return m.embeds
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEmbeds sets the attachements of the message.
|
// SetEmbeds sets the embeds of the message.
|
||||||
func (m *Msg) SetEmbeds(ff []*File) {
|
func (m *Msg) SetEmbeds(ff []*File) {
|
||||||
m.embeds = ff
|
m.embeds = ff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnsetAllEmbeds unset the embeds of the message.
|
||||||
|
func (m *Msg) UnsetAllEmbeds() {
|
||||||
|
m.embeds = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnsetAllParts unset the embeds and attachments of the message.
|
||||||
|
func (m *Msg) UnsetAllParts() {
|
||||||
|
m.UnsetAllAttachments()
|
||||||
|
m.UnsetAllEmbeds()
|
||||||
|
}
|
||||||
|
|
||||||
// SetBodyString sets the body of the message.
|
// SetBodyString sets the body of the message.
|
||||||
func (m *Msg) SetBodyString(ct ContentType, b string, o ...PartOption) {
|
func (m *Msg) SetBodyString(ct ContentType, b string, o ...PartOption) {
|
||||||
buf := bytes.NewBufferString(b)
|
buf := bytes.NewBufferString(b)
|
||||||
|
|
116
msg_test.go
116
msg_test.go
|
@ -1400,6 +1400,40 @@ func TestMsg_SetAttachments(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestMsg_UnsetAllAttachments tests the Msg.UnsetAllAttachments method
|
||||||
|
func TestMsg_UnsetAllAttachments(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
attachments []string
|
||||||
|
}{
|
||||||
|
{"File: one file", []string{"README.md"}},
|
||||||
|
{"File: two files", []string{"README.md", "doc.go"}},
|
||||||
|
{"File: nil", nil},
|
||||||
|
}
|
||||||
|
m := NewMsg()
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var files []*File
|
||||||
|
for _, f := range tt.attachments {
|
||||||
|
files = append(files, &File{Name: f})
|
||||||
|
}
|
||||||
|
m.SetAttachements(files)
|
||||||
|
|
||||||
|
if len(m.attachments) != len(files) {
|
||||||
|
t.Errorf("SetAttachements() failed. Number of attachments expected: %d, got: %d", len(files),
|
||||||
|
len(m.attachments))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.UnsetAllAttachments()
|
||||||
|
if m.attachments != nil {
|
||||||
|
t.Errorf("UnsetAllAttachments() failed. The attachments file's pointer is not nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.Reset()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestMsg_GetEmbeds tests the Msg.GetEmbeds method
|
// TestMsg_GetEmbeds tests the Msg.GetEmbeds method
|
||||||
func TestMsg_GetEmbeds(t *testing.T) {
|
func TestMsg_GetEmbeds(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -1493,6 +1527,88 @@ func TestMsg_SetEmbeds(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestMsg_UnsetAllEmbeds tests the Msg.TestMsg_UnsetAllEmbeds method
|
||||||
|
func TestMsg_UnsetAllEmbeds(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
embeds []string
|
||||||
|
}{
|
||||||
|
{"File: one file", []string{"README.md"}},
|
||||||
|
{"File: two files", []string{"README.md", "doc.go"}},
|
||||||
|
{"File: nil", nil},
|
||||||
|
}
|
||||||
|
m := NewMsg()
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var files []*File
|
||||||
|
for _, f := range tt.embeds {
|
||||||
|
files = append(files, &File{Name: f})
|
||||||
|
}
|
||||||
|
m.SetEmbeds(files)
|
||||||
|
if len(m.embeds) != len(files) {
|
||||||
|
t.Errorf("SetEmbeds() failed. Number of embedded files expected: %d, got: %d", len(files),
|
||||||
|
len(m.embeds))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.UnsetAllEmbeds()
|
||||||
|
if m.embeds != nil {
|
||||||
|
t.Errorf("UnsetAllEmbeds() failed. The embeds file's point is not nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.Reset()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMsg_UnsetAllParts tests the Msg.TestMsg_UnsetAllParts method
|
||||||
|
func TestMsg_UnsetAllParts(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
attachments []string
|
||||||
|
embeds []string
|
||||||
|
}{
|
||||||
|
{"File: both is exist", []string{"README.md"}, []string{"doc.go"}},
|
||||||
|
{"File: both is nil", nil, nil},
|
||||||
|
{"File: attachment exist, embed nil", []string{"README.md"}, nil},
|
||||||
|
{"File: attachment nil, embed exist", nil, []string{"README.md"}},
|
||||||
|
}
|
||||||
|
m := NewMsg()
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var attachments []*File
|
||||||
|
for _, f := range tt.attachments {
|
||||||
|
attachments = append(attachments, &File{Name: f})
|
||||||
|
}
|
||||||
|
m.SetAttachements(attachments)
|
||||||
|
if len(m.attachments) != len(attachments) {
|
||||||
|
t.Errorf("SetAttachements() failed. Number of attachments files expected: %d, got: %d",
|
||||||
|
len(attachments), len(m.attachments))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var embeds []*File
|
||||||
|
for _, f := range tt.embeds {
|
||||||
|
embeds = append(embeds, &File{Name: f})
|
||||||
|
}
|
||||||
|
m.SetEmbeds(embeds)
|
||||||
|
if len(m.embeds) != len(embeds) {
|
||||||
|
t.Errorf("SetEmbeds() failed. Number of embedded files expected: %d, got: %d", len(embeds),
|
||||||
|
len(m.embeds))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.UnsetAllParts()
|
||||||
|
if m.attachments != nil {
|
||||||
|
t.Errorf("UnsetAllParts() failed. The attachments file's point is not nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.embeds != nil {
|
||||||
|
t.Errorf("UnsetAllParts() failed. The embeds file's point is not nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.Reset()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestMsg_AttachFromEmbedFS tests the Msg.AttachFromEmbedFS and the WithFilename FileOption method
|
// TestMsg_AttachFromEmbedFS tests the Msg.AttachFromEmbedFS and the WithFilename FileOption method
|
||||||
func TestMsg_AttachFromEmbedFS(t *testing.T) {
|
func TestMsg_AttachFromEmbedFS(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
Loading…
Reference in a new issue