mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-12 17:02:55 +01:00
Merge pull request #108 from wneessen/feature/107_provide-more-ways-for-middleware-to-interact-with-mail-parts
Introducing Msg part deletion
This commit is contained in:
commit
3c0f5d03d8
4 changed files with 33 additions and 2 deletions
8
msg.go
8
msg.go
|
@ -989,7 +989,13 @@ func (m *Msg) encodeString(s string) string {
|
|||
|
||||
// hasAlt returns true if the Msg has more than one part
|
||||
func (m *Msg) hasAlt() bool {
|
||||
return len(m.parts) > 1
|
||||
c := 0
|
||||
for _, p := range m.parts {
|
||||
if !p.del {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c > 1
|
||||
}
|
||||
|
||||
// hasMixed returns true if the Msg has mixed parts
|
||||
|
|
|
@ -102,7 +102,9 @@ func (mw *msgWriter) writeMsg(m *Msg) {
|
|||
}
|
||||
|
||||
for _, p := range m.parts {
|
||||
mw.writePart(p, m.charset)
|
||||
if !p.del {
|
||||
mw.writePart(p, m.charset)
|
||||
}
|
||||
}
|
||||
if m.hasAlt() {
|
||||
mw.stopMP()
|
||||
|
|
7
part.go
7
part.go
|
@ -16,6 +16,7 @@ type PartOption func(*Part)
|
|||
type Part struct {
|
||||
ctype ContentType
|
||||
enc Encoding
|
||||
del bool
|
||||
w func(io.Writer) (int64, error)
|
||||
}
|
||||
|
||||
|
@ -64,6 +65,12 @@ func (p *Part) SetWriteFunc(w func(io.Writer) (int64, error)) {
|
|||
p.w = w
|
||||
}
|
||||
|
||||
// Delete removes the current part from the parts list of the Msg by setting the
|
||||
// del flag to true. The msgWriter will skip it then
|
||||
func (p *Part) Delete() {
|
||||
p.del = true
|
||||
}
|
||||
|
||||
// WithPartEncoding overrides the default Part encoding
|
||||
func WithPartEncoding(e Encoding) PartOption {
|
||||
return func(p *Part) {
|
||||
|
|
16
part_test.go
16
part_test.go
|
@ -241,6 +241,22 @@ func TestPart_SetContent(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestPart_Delete tests Part.Delete
|
||||
func TestPart_Delete(t *testing.T) {
|
||||
c := "This is a test with ümläutß"
|
||||
m := NewMsg()
|
||||
m.SetBodyString(TypeTextPlain, c)
|
||||
pl, err := getPartList(m)
|
||||
if err != nil {
|
||||
t.Errorf("failed: %s", err)
|
||||
return
|
||||
}
|
||||
pl[0].Delete()
|
||||
if !pl[0].del {
|
||||
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].del)
|
||||
}
|
||||
}
|
||||
|
||||
// getPartList is a helper function
|
||||
func getPartList(m *Msg) ([]*Part, error) {
|
||||
pl := m.GetParts()
|
||||
|
|
Loading…
Reference in a new issue