Fixed name encoding in adress headers

Closes #24.
This commit is contained in:
Alexandre Cesaro 2015-03-14 20:37:43 +01:00
parent cb55422c78
commit 19744cb73d
2 changed files with 30 additions and 8 deletions

View File

@ -12,7 +12,7 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/alexcesaro/quotedprintable.v1" "gopkg.in/alexcesaro/quotedprintable.v2"
) )
// Message represents an email. // Message represents an email.
@ -104,15 +104,11 @@ const (
// SetHeader sets a value to the given header field. // SetHeader sets a value to the given header field.
func (msg *Message) SetHeader(field string, value ...string) { func (msg *Message) SetHeader(field string, value ...string) {
for i := range value { for i := range value {
value[i] = msg.encodeHeader(value[i]) value[i] = encodeHeader(msg.hEncoder, value[i])
} }
msg.header[field] = value msg.header[field] = value
} }
func (msg *Message) encodeHeader(value string) string {
return msg.hEncoder.Encode(value)
}
// SetHeaders sets the message headers. // SetHeaders sets the message headers.
// //
// Example: // Example:
@ -138,10 +134,15 @@ func (msg *Message) FormatAddress(address, name string) string {
buf := getBuffer() buf := getBuffer()
defer putBuffer(buf) defer putBuffer(buf)
n := msg.encodeHeader(name) if !quotedprintable.NeedsEncoding(name) {
if n == name {
quote(buf, name) quote(buf, name)
} else { } else {
var n string
if hasSpecials(name) {
n = encodeHeader(quotedprintable.B.NewHeaderEncoder(msg.charset), name)
} else {
n = encodeHeader(msg.hEncoder, name)
}
buf.WriteString(n) buf.WriteString(n)
} }
buf.WriteString(" <") buf.WriteString(" <")
@ -295,6 +296,25 @@ func quote(buf *bytes.Buffer, text string) {
buf.WriteByte('"') buf.WriteByte('"')
} }
func hasSpecials(text string) bool {
for i := 0; i < len(text); i++ {
switch c := text[i]; c {
case '(', ')', '<', '>', '[', ']', ':', ';', '@', '\\', ',', '.', '"':
return true
}
}
return false
}
func encodeHeader(enc *quotedprintable.HeaderEncoder, value string) string {
if !quotedprintable.NeedsEncoding(value) {
return value
}
return enc.Encode(value)
}
var bufPool = sync.Pool{ var bufPool = sync.Pool{
New: func() interface{} { New: func() interface{} {
return new(bytes.Buffer) return new(bytes.Buffer)

View File

@ -22,6 +22,7 @@ func TestMessage(t *testing.T) {
msg.SetAddressHeader("From", "from@example.com", "Señor From") msg.SetAddressHeader("From", "from@example.com", "Señor From")
msg.SetHeader("To", msg.FormatAddress("to@example.com", "Señor To"), "tobis@example.com") msg.SetHeader("To", msg.FormatAddress("to@example.com", "Señor To"), "tobis@example.com")
msg.SetAddressHeader("Cc", "cc@example.com", "A, B") msg.SetAddressHeader("Cc", "cc@example.com", "A, B")
msg.SetAddressHeader("X-To", "ccbis@example.com", "à, b")
msg.SetDateHeader("X-Date", stubNow()) msg.SetDateHeader("X-Date", stubNow())
msg.SetHeader("X-Date-2", msg.FormatDate(stubNow())) msg.SetHeader("X-Date-2", msg.FormatDate(stubNow()))
msg.SetHeader("Subject", "¡Hola, señor!") msg.SetHeader("Subject", "¡Hola, señor!")
@ -40,6 +41,7 @@ func TestMessage(t *testing.T) {
content: "From: =?UTF-8?Q?Se=C3=B1or_From?= <from@example.com>\r\n" + content: "From: =?UTF-8?Q?Se=C3=B1or_From?= <from@example.com>\r\n" +
"To: =?UTF-8?Q?Se=C3=B1or_To?= <to@example.com>, tobis@example.com\r\n" + "To: =?UTF-8?Q?Se=C3=B1or_To?= <to@example.com>, tobis@example.com\r\n" +
"Cc: \"A, B\" <cc@example.com>\r\n" + "Cc: \"A, B\" <cc@example.com>\r\n" +
"X-To: =?UTF-8?B?w6AsIGI=?= <ccbis@example.com>\r\n" +
"X-Date: Wed, 25 Jun 2014 17:46:00 +0000\r\n" + "X-Date: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
"X-Date-2: Wed, 25 Jun 2014 17:46:00 +0000\r\n" + "X-Date-2: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
"X-Headers: Test, =?UTF-8?Q?Caf=C3=A9?=\r\n" + "X-Headers: Test, =?UTF-8?Q?Caf=C3=A9?=\r\n" +