Added a new encoding: Unencoded

This commit is contained in:
alexcesaro 2014-10-28 08:35:50 +01:00
parent 4fcb155d12
commit b4e3113e2d
3 changed files with 30 additions and 5 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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{