From 087556715f372d60947ceea230cdf7cd1af91313 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Tue, 12 Mar 2024 12:05:24 +0530 Subject: [PATCH] Add support for HeaderEncodingNone --- email.go | 9 +++++---- header.go | 31 ++++++------------------------- header_test.go | 4 ++-- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/email.go b/email.go index 9af82f4..0a109ce 100644 --- a/email.go +++ b/email.go @@ -115,7 +115,7 @@ type headerEncoding int const ( // HeaderEncodingNone turns off encoding on the message headers - HeaderEncodingNone encoding = iota + HeaderEncodingNone headerEncoding = iota // TODO: Add Base64 encoding // HeaderEncodingBase64 sets the message header encoding to base64 @@ -222,9 +222,10 @@ func (dsn DSN) String() string { // NewMSG creates a new email. It uses UTF-8 by default. All charsets: http://webcheatsheet.com/HTML/character_sets_list.php func NewMSG() *Email { email := &Email{ - headers: make(textproto.MIMEHeader), - Charset: "UTF-8", - Encoding: EncodingQuotedPrintable, + headers: make(textproto.MIMEHeader), + Charset: "UTF-8", + Encoding: EncodingQuotedPrintable, + HeaderEncoding: HeaderEncodingQ, } email.AddHeader("MIME-Version", "1.0") diff --git a/header.go b/header.go index 59acda6..923be5b 100644 --- a/header.go +++ b/header.go @@ -27,8 +27,9 @@ func newEncoder(w io.Writer, c string, encoding headerEncoding, u int) *encoder return &encoder{bufio.NewWriter(w), strings.ToUpper(c), encoding, u} } -// encode encodes p using the "Q" encoding and writes it to the underlying -// io.Writer. It limits line length to 75 characters. +// encode encodes p using the encoding scheme specified in e +// If all chars are printable ascii chars, no encoding is performed. +// Limits line length to 75 characters and folds lines as necessary. func (e *encoder) encode(p []byte) (n int, err error) { var output bytes.Buffer allPrintable := true @@ -49,7 +50,7 @@ func (e *encoder) encode(p []byte) (n int, err error) { } // all characters are printable. just do line folding - if allPrintable { + if allPrintable || e.encoding == HeaderEncodingNone { text := string(p) words := strings.Split(text, " ") @@ -58,10 +59,6 @@ func (e *encoder) encode(p []byte) (n int, err error) { // split the line where necessary for _, word := range words { - /*fmt.Println("Current Line:",lineBuffer) - fmt.Println("Here: Max:", maxLineLength ,"Buffer Length:", len(lineBuffer), "Used Chars:", e.usedChars, "Length Encoded Char:",len(word)) - fmt.Println("----------")*/ - newWord := "" if !firstWord { newWord += " " @@ -69,7 +66,7 @@ func (e *encoder) encode(p []byte) (n int, err error) { newWord += word // check line length - if (e.usedChars+len(lineBuffer)+len(newWord) /*+len(" ")+len(word)*/) > maxLineLength && (lineBuffer != "" || e.usedChars != 0) { + if (e.usedChars+len(lineBuffer)+len(newWord)) > maxLineLength && (lineBuffer != "" || e.usedChars != 0) { output.WriteString(lineBuffer + "\r\n") // first word on newline needs a space in front @@ -79,29 +76,17 @@ func (e *encoder) encode(p []byte) (n int, err error) { lineBuffer = " " } - //firstLine = false - //firstWord = true // reset since not on the first line anymore e.usedChars = 0 } - /*if !firstWord { - lineBuffer += " " - }*/ - lineBuffer += newWord /*word*/ - firstWord = false - - // reset since not on the first line anymore - /*if !firstLine { - e.usedChars = 0 - }*/ } output.WriteString(lineBuffer) - } else { + // else block can only be HeaderEncodingQ as of now firstLine := true // A single encoded word can not be longer than 75 characters @@ -118,10 +103,6 @@ func (e *encoder) encode(p []byte) (n int, err error) { // encode the character encodedChar, runeLength := encode(p, i) - /*fmt.Println("Current Line:",lineBuffer) - fmt.Println("Here: Max:", maxLineLength ,"Buffer Length:", len(lineBuffer), "Used Chars:", e.usedChars, "Length Encoded Char:",len(encodedChar)) - fmt.Println("----------")*/ - // Check line length if len(lineBuffer)+e.usedChars+len(encodedChar) > (maxLineLength - len(wordEnd)) { output.WriteString(lineBuffer + wordEnd + "\r\n") diff --git a/header_test.go b/header_test.go index 1806c2f..a2c2c78 100644 --- a/header_test.go +++ b/header_test.go @@ -13,11 +13,11 @@ func TestWriter(t *testing.T) { func testWriter(t *testing.T, binary bool) { utf8 := "utf-8" - qp := EncodingQuotedPrintable + qp := HeaderEncodingQ tests := []struct { charset string - encoding encoding + encoding headerEncoding usedChars int in, want string }{