diff --git a/export.go b/export.go index 99038ad..328b4bb 100644 --- a/export.go +++ b/export.go @@ -29,11 +29,7 @@ func (msg *Message) Export() *mail.Message { for _, part := range msg.parts { h := make(map[string][]string) h["Content-Type"] = []string{part.contentType + "; charset=" + msg.charset} - if msg.encoding == Base64 { - h["Content-Transfer-Encoding"] = []string{string(Base64)} - } else { - h["Content-Transfer-Encoding"] = []string{string(QuotedPrintable)} - } + h["Content-Transfer-Encoding"] = []string{string(msg.encoding)} w.write(h, part.body.Bytes(), msg.encoding) } @@ -166,6 +162,8 @@ func (w *messageWriter) writeBody(body []byte, enc Encoding) { writer := base64.NewEncoder(base64.StdEncoding, newBase64LineWriter(subWriter)) writer.Write(body) writer.Close() + } else if enc == Unencoded { + subWriter.Write(body) } else { writer := quotedprintable.NewEncoder(newQpLineWriter(subWriter)) writer.Write(body) diff --git a/gomail.go b/gomail.go index 79d4714..c5cb85f 100644 --- a/gomail.go +++ b/gomail.go @@ -95,6 +95,9 @@ const ( QuotedPrintable Encoding = "quoted-printable" // Base64 represents the base64 encoding as defined in RFC 2045. Base64 Encoding = "base64" + // Unencoded can be used to avoid encoding the body of an email. The headers + // will still be encoded using quoted-printable encoding. + Unencoded Encoding = "8bit" ) // SetHeader sets a value to the given header field. diff --git a/gomail_test.go b/gomail_test.go index 57881ee..74c8851 100644 --- a/gomail_test.go +++ b/gomail_test.go @@ -95,6 +95,30 @@ func TestCustomMessage(t *testing.T) { testMessage(t, msg, 0, want) } +func TestUnencodedMessage(t *testing.T) { + msg := NewMessage(SetEncoding(Unencoded)) + msg.SetHeaders(map[string][]string{ + "From": {"from@example.com"}, + "To": {"to@example.com"}, + "Subject": {"Café"}, + }) + msg.SetBody("text/html", "¡Hola, señor!") + + want := message{ + from: "from@example.com", + to: []string{"to@example.com"}, + content: "From: from@example.com\r\n" + + "To: to@example.com\r\n" + + "Subject: =?UTF-8?Q?Caf=C3=A9?=\r\n" + + "Content-Type: text/html; charset=UTF-8\r\n" + + "Content-Transfer-Encoding: 8bit\r\n" + + "\r\n" + + "¡Hola, señor!", + } + + testMessage(t, msg, 0, want) +} + func TestRecipients(t *testing.T) { msg := NewMessage() msg.SetHeaders(map[string][]string{