diff --git a/message.go b/message.go index 2e2ce7c..95fd7b8 100644 --- a/message.go +++ b/message.go @@ -26,8 +26,8 @@ type Message struct { type header map[string][]string type part struct { - contentType string - copier func(io.Writer) error + header header + copier func(io.Writer) error } // NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding @@ -201,7 +201,7 @@ func (m *Message) DelHeader(field string) { func (m *Message) SetBody(contentType, body string) { m.parts = []part{ { - contentType: contentType, + header: m.getPartHeader(contentType), copier: func(w io.Writer) error { _, err := io.WriteString(w, body) return err @@ -223,7 +223,7 @@ func (m *Message) SetBody(contentType, body string) { func (m *Message) AddAlternative(contentType, body string) { m.parts = append(m.parts, part{ - contentType: contentType, + header: m.getPartHeader(contentType), copier: func(w io.Writer) error { _, err := io.WriteString(w, body) return err @@ -244,12 +244,19 @@ func (m *Message) AddAlternative(contentType, body string) { func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) { m.parts = []part{ { - contentType: contentType, - copier: f, + header: m.getPartHeader(contentType), + copier: f, }, } } +func (m *Message) getPartHeader(contentType string) header { + return map[string][]string{ + "Content-Type": {contentType + "; charset=" + m.charset}, + "Content-Transfer-Encoding": {string(m.encoding)}, + } +} + // A File represents a file that can be attached or embedded in an email. type File struct { // Name represents the base name of the file. If the file is attached to the diff --git a/writeto.go b/writeto.go index cdc10a0..625d63e 100644 --- a/writeto.go +++ b/writeto.go @@ -38,11 +38,7 @@ func (w *messageWriter) writeMessage(m *Message) { w.openMultipart("alternative") } for _, part := range m.parts { - contentType := part.contentType + "; charset=" + m.charset - w.writeHeaders(map[string][]string{ - "Content-Type": {contentType}, - "Content-Transfer-Encoding": {string(m.encoding)}, - }) + w.writeHeaders(part.header) w.writeBody(part.copier, m.encoding) } if m.hasAlternativePart() {