fix(GODT-2479): Ensure messages always have a text body part

Proton Backend requires that all messages have at least one text/plain
or text/html body part, even if it is empty.
This commit is contained in:
Leander Beernaert
2023-03-14 10:34:03 +01:00
parent 48274ee178
commit 8b33d56b59
4 changed files with 222 additions and 2 deletions

View File

@ -20,6 +20,7 @@ package parser
import (
"fmt"
"io"
"strings"
"github.com/emersion/go-message"
"github.com/sirupsen/logrus"
@ -81,6 +82,39 @@ func (p *Parser) AttachPublicKey(key, keyName string) {
})
}
func (p *Parser) AttachEmptyTextPartIfNoneExists() {
root := p.Root()
if root.isMultipartMixed() {
for _, v := range root.children {
// Must be an attachment of sorts, skip.
if v.Header.Has("Content-Disposition") {
continue
}
contentType, _, err := v.Header.ContentType()
if err == nil && strings.HasPrefix(contentType, "text/") {
// Message already has text part
return
}
}
} else {
contentType, _, err := root.Header.ContentType()
if err == nil && strings.HasPrefix(contentType, "text/") {
// Message already has text part
return
}
}
h := message.Header{}
h.Set("Content-Type", "text/plain;charset=utf8")
h.Set("Content-Transfer-Encoding", "quoted-printable")
p.Root().AddChild(&Part{
Header: h,
Body: nil,
})
}
// Section returns the message part referred to by the given section. A section
// is zero or more integers. For example, section 1.2.3 will return the third
// part of the second part of the first part of the message.