Fix to emit at most 76 chars per line

From RFC 2045 (and also from looking at other packages), it appears that
line length limit is actually 76 characters, not 78. Also, this fixes
attachments broken before for a certain webmail client (roundcube).

Closes #18.
This commit is contained in:
Mateusz Czapliński 2015-01-20 13:15:52 +01:00 committed by Alexandre Cesaro
parent 512e469f62
commit d7294067b8
2 changed files with 17 additions and 16 deletions

View File

@ -174,10 +174,11 @@ func (w *messageWriter) export() *mail.Message {
return &mail.Message{Header: w.header, Body: w.buf} return &mail.Message{Header: w.header, Body: w.buf}
} }
// As defined in RFC 5322, 2.1.1. // As required by RFC 2045, 6.7. (page 21) for quoted-printable, and
const maxLineLen = 78 // RFC 2045, 6.8. (page 25) for base64.
const maxLineLen = 76
// base64LineWriter limits text encoded in base64 to 78 characters per line // base64LineWriter limits text encoded in base64 to 76 characters per line
type base64LineWriter struct { type base64LineWriter struct {
w io.Writer w io.Writer
lineLen int lineLen int
@ -203,7 +204,7 @@ func (w *base64LineWriter) Write(p []byte) (int, error) {
return n + len(p), nil return n + len(p), nil
} }
// qpLineWriter limits text encoded in quoted-printable to 78 characters per // qpLineWriter limits text encoded in quoted-printable to 76 characters per
// line // line
type qpLineWriter struct { type qpLineWriter struct {
w io.Writer w io.Writer

View File

@ -432,13 +432,13 @@ func TestQpLineLength(t *testing.T) {
msg.SetHeader("From", "from@example.com") msg.SetHeader("From", "from@example.com")
msg.SetHeader("To", "to@example.com") msg.SetHeader("To", "to@example.com")
msg.SetBody("text/plain", msg.SetBody("text/plain",
strings.Repeat("0", 79)+"\r\n"+ strings.Repeat("0", 77)+"\r\n"+
strings.Repeat("0", 78)+"à\r\n"+
strings.Repeat("0", 77)+"à\r\n"+
strings.Repeat("0", 76)+"à\r\n"+ strings.Repeat("0", 76)+"à\r\n"+
strings.Repeat("0", 75)+"à\r\n"+ strings.Repeat("0", 75)+"à\r\n"+
strings.Repeat("0", 78)+"\r\n"+ strings.Repeat("0", 74)+"à\r\n"+
strings.Repeat("0", 79)+"\n") strings.Repeat("0", 73)+"à\r\n"+
strings.Repeat("0", 76)+"\r\n"+
strings.Repeat("0", 77)+"\n")
want := message{ want := message{
from: "from@example.com", from: "from@example.com",
@ -448,13 +448,13 @@ func TestQpLineLength(t *testing.T) {
"Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" +
"\r\n" + "\r\n" +
strings.Repeat("0", 78) + "=\r\n0\r\n" + strings.Repeat("0", 76) + "=\r\n0\r\n" +
strings.Repeat("0", 78) + "=\r\n=C3=A0\r\n" +
strings.Repeat("0", 77) + "=\r\n=C3=A0\r\n" +
strings.Repeat("0", 76) + "=\r\n=C3=A0\r\n" + strings.Repeat("0", 76) + "=\r\n=C3=A0\r\n" +
strings.Repeat("0", 75) + "=C3=\r\n=A0\r\n" + strings.Repeat("0", 75) + "=\r\n=C3=A0\r\n" +
strings.Repeat("0", 78) + "\r\n" + strings.Repeat("0", 74) + "=\r\n=C3=A0\r\n" +
strings.Repeat("0", 78) + "=\r\n0\n", strings.Repeat("0", 73) + "=C3=\r\n=A0\r\n" +
strings.Repeat("0", 76) + "\r\n" +
strings.Repeat("0", 76) + "=\r\n0\n",
} }
testMessage(t, msg, 0, want) testMessage(t, msg, 0, want)
@ -474,7 +474,7 @@ func TestBase64LineLength(t *testing.T) {
"Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n" +
"Content-Transfer-Encoding: base64\r\n" + "Content-Transfer-Encoding: base64\r\n" +
"\r\n" + "\r\n" +
strings.Repeat("MDAw", 19) + "MA\r\n==", strings.Repeat("MDAw", 19) + "\r\nMA==",
} }
testMessage(t, msg, 0, want) testMessage(t, msg, 0, want)