Introducing Msg part deletion

This PR introduces a new struct field for the message parts: `del`

If the del flag is set to `true`, the msgWriter will ignore this part during the writing process.

Additionally, the `part` has now a `Delete` method that lets the user mark the part as deleted

This allows middleware to take further control of the Msg and is part of #107
This commit is contained in:
Winni Neessen 2023-01-28 14:39:14 +01:00
parent 91bfed3f3d
commit 9724a2b523
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 17 additions and 2 deletions

8
msg.go
View file

@ -989,7 +989,13 @@ func (m *Msg) encodeString(s string) string {
// hasAlt returns true if the Msg has more than one part // hasAlt returns true if the Msg has more than one part
func (m *Msg) hasAlt() bool { 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 // hasMixed returns true if the Msg has mixed parts

View file

@ -102,7 +102,9 @@ func (mw *msgWriter) writeMsg(m *Msg) {
} }
for _, p := range m.parts { for _, p := range m.parts {
mw.writePart(p, m.charset) if !p.del {
mw.writePart(p, m.charset)
}
} }
if m.hasAlt() { if m.hasAlt() {
mw.stopMP() mw.stopMP()

View file

@ -16,6 +16,7 @@ type PartOption func(*Part)
type Part struct { type Part struct {
ctype ContentType ctype ContentType
enc Encoding enc Encoding
del bool
w func(io.Writer) (int64, error) w func(io.Writer) (int64, error)
} }
@ -64,6 +65,12 @@ func (p *Part) SetWriteFunc(w func(io.Writer) (int64, error)) {
p.w = w 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 // WithPartEncoding overrides the default Part encoding
func WithPartEncoding(e Encoding) PartOption { func WithPartEncoding(e Encoding) PartOption {
return func(p *Part) { return func(p *Part) {