Add Charset support for message parts

Charset support has been added in the 'Part' struct. A 'SetCharset' method and a 'WithPartCharset' option have been added to override the default Part charset. The 'writePart' function in msgWriter now accommodates the charset defined at the Part level, defaulting to the previous functionality if not set.
This commit is contained in:
Winni Neessen 2024-01-31 16:28:41 +01:00
parent 7dced4b056
commit ef86a1a1fb
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 19 additions and 1 deletions

1
msg.go
View file

@ -1141,6 +1141,7 @@ func (m *Msg) hasPGPType() bool {
func (m *Msg) newPart(ct ContentType, o ...PartOption) *Part {
p := &Part{
ctype: ct,
cset: m.charset,
enc: m.encoding,
}

View file

@ -245,7 +245,11 @@ func (mw *msgWriter) newPart(h map[string][]string) {
// writePart writes the corresponding part to the Msg body
func (mw *msgWriter) writePart(p *Part, cs Charset) {
ct := fmt.Sprintf("%s; charset=%s", p.ctype, cs)
pcs := p.cset
if pcs.String() == "" {
pcs = cs
}
ct := fmt.Sprintf("%s; charset=%s", p.ctype, pcs)
cte := p.enc.String()
if mw.d == 0 {
mw.writeHeader(HeaderContentType, ct)

13
part.go
View file

@ -15,6 +15,7 @@ type PartOption func(*Part)
// Part is a part of the Msg
type Part struct {
ctype ContentType
cset Charset
desc string
enc Encoding
del bool
@ -61,6 +62,11 @@ func (p *Part) SetContentType(c ContentType) {
p.ctype = c
}
// SetCharset overrides the Charset of the Part
func (p *Part) SetCharset(c Charset) {
p.cset = c
}
// SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message
func (p *Part) SetEncoding(e Encoding) {
p.enc = e
@ -82,6 +88,13 @@ func (p *Part) Delete() {
p.del = true
}
// WithPartCharset overrides the default Part charset
func WithPartCharset(c Charset) PartOption {
return func(p *Part) {
p.cset = c
}
}
// WithPartEncoding overrides the default Part encoding
func WithPartEncoding(e Encoding) PartOption {
return func(p *Part) {