Changed the internal representation of a part

This commit is contained in:
Alexandre Cesaro 2015-07-23 23:22:08 +02:00
parent 71e3f5a0b4
commit 4586cb75ac
2 changed files with 14 additions and 11 deletions

View File

@ -26,8 +26,8 @@ type Message struct {
type header map[string][]string type header map[string][]string
type part struct { type part struct {
contentType string header header
copier func(io.Writer) error copier func(io.Writer) error
} }
// NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding // 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) { func (m *Message) SetBody(contentType, body string) {
m.parts = []part{ m.parts = []part{
{ {
contentType: contentType, header: m.getPartHeader(contentType),
copier: func(w io.Writer) error { copier: func(w io.Writer) error {
_, err := io.WriteString(w, body) _, err := io.WriteString(w, body)
return err return err
@ -223,7 +223,7 @@ func (m *Message) SetBody(contentType, body string) {
func (m *Message) AddAlternative(contentType, body string) { func (m *Message) AddAlternative(contentType, body string) {
m.parts = append(m.parts, m.parts = append(m.parts,
part{ part{
contentType: contentType, header: m.getPartHeader(contentType),
copier: func(w io.Writer) error { copier: func(w io.Writer) error {
_, err := io.WriteString(w, body) _, err := io.WriteString(w, body)
return err return err
@ -244,12 +244,19 @@ func (m *Message) AddAlternative(contentType, body string) {
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) { func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) {
m.parts = []part{ m.parts = []part{
{ {
contentType: contentType, header: m.getPartHeader(contentType),
copier: f, 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. // A File represents a file that can be attached or embedded in an email.
type File struct { type File struct {
// Name represents the base name of the file. If the file is attached to the // Name represents the base name of the file. If the file is attached to the

View File

@ -38,11 +38,7 @@ func (w *messageWriter) writeMessage(m *Message) {
w.openMultipart("alternative") w.openMultipart("alternative")
} }
for _, part := range m.parts { for _, part := range m.parts {
contentType := part.contentType + "; charset=" + m.charset w.writeHeaders(part.header)
w.writeHeaders(map[string][]string{
"Content-Type": {contentType},
"Content-Transfer-Encoding": {string(m.encoding)},
})
w.writeBody(part.copier, m.encoding) w.writeBody(part.copier, m.encoding)
} }
if m.hasAlternativePart() { if m.hasAlternativePart() {