Skip to content

Commit

Permalink
Add support for HeaderEncodingNone
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Mar 12, 2024
1 parent 643cddb commit 0875567
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
9 changes: 5 additions & 4 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
31 changes: 6 additions & 25 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, " ")

Expand All @@ -58,18 +59,14 @@ 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 += " "
}
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
Expand All @@ -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
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down

0 comments on commit 0875567

Please sign in to comment.