Browse Source

Make message bodies []byte instead of string

Jed Denlea 11 years ago
parent
commit
a8f65cf632
2 changed files with 19 additions and 20 deletions
  1. 7 8
      email.go
  2. 12 12
      email_test.go

+ 7 - 8
email.go

@@ -31,8 +31,8 @@ type Email struct {
 	Bcc         []string
 	Cc          []string
 	Subject     string
-	Text        string // Plaintext message (optional)
-	HTML        string // Html message (optional)
+	Text        []byte // Plaintext message (optional)
+	HTML        []byte // Html message (optional)
 	Headers     textproto.MIMEHeader
 	Attachments []*Attachment
 	ReadReceipt []string
@@ -109,7 +109,7 @@ func (e *Email) Bytes() ([]byte, error) {
 	fmt.Fprintf(buff, "--%s\r\n", w.Boundary())
 	header := textproto.MIMEHeader{}
 	// Check to see if there is a Text or HTML field
-	if e.Text != "" || e.HTML != "" {
+	if len(e.Text) > 0 || len(e.HTML) > 0 {
 		subWriter := multipart.NewWriter(buff)
 		// Create the multipart alternative part
 		header.Set("Content-Type", fmt.Sprintf("multipart/alternative;\r\n boundary=%s\r\n", subWriter.Boundary()))
@@ -118,7 +118,7 @@ func (e *Email) Bytes() ([]byte, error) {
 			return nil, fmt.Errorf("Failed to render multipart message headers: %s", err)
 		}
 		// Create the body sections
-		if e.Text != "" {
+		if len(e.Text) > 0 {
 			header.Set("Content-Type", fmt.Sprintf("text/plain; charset=UTF-8"))
 			header.Set("Content-Transfer-Encoding", "quoted-printable")
 			if _, err := subWriter.CreatePart(header); err != nil {
@@ -129,7 +129,7 @@ func (e *Email) Bytes() ([]byte, error) {
 				return nil, err
 			}
 		}
-		if e.HTML != "" {
+		if len(e.HTML) > 0 {
 			header.Set("Content-Type", fmt.Sprintf("text/html; charset=UTF-8"))
 			header.Set("Content-Transfer-Encoding", "quoted-printable")
 			if _, err := subWriter.CreatePart(header); err != nil {
@@ -189,11 +189,10 @@ type Attachment struct {
 }
 
 // quotePrintEncode writes the quoted-printable text to the IO Writer (according to RFC 2045)
-func quotePrintEncode(w io.Writer, s string) error {
+func quotePrintEncode(w io.Writer, body []byte) error {
 	var buf [3]byte
 	mc := 0
-	for i := 0; i < len(s); i++ {
-		c := s[i]
+	for _, c := range body {
 		// We're assuming Unix style text formats as input (LF line break), and
 		// quoted-printble uses CRLF line breaks. (Literal CRs will become
 		// "=0D", but probably shouldn't be there to begin with!)

+ 12 - 12
email_test.go

@@ -21,8 +21,8 @@ func TestEmailTextHtmlAttachment(t *testing.T) {
 	e.Bcc = []string{"test_bcc@example.com"}
 	e.Cc = []string{"test_cc@example.com"}
 	e.Subject = "Awesome Subject"
-	e.Text = "Text Body is, of course, supported!\n"
-	e.HTML = "<h1>Fancy Html is supported, too!</h1>\n"
+	e.Text = []byte("Text Body is, of course, supported!\n")
+	e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
 	e.Attach(bytes.NewBufferString("Rad attachement"), "rad.txt", "text/plain; charset=utf-8")
 
 	raw, err := e.Bytes()
@@ -112,8 +112,8 @@ func ExampleGmail() {
 	e.Bcc = []string{"test_bcc@example.com"}
 	e.Cc = []string{"test_cc@example.com"}
 	e.Subject = "Awesome Subject"
-	e.Text = "Text Body is, of course, supported!"
-	e.HTML = "<h1>Fancy Html is supported, too!</h1>"
+	e.Text = []byte("Text Body is, of course, supported!\n")
+	e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
 	e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
 }
 
@@ -139,35 +139,35 @@ func Test_base64Wrap(t *testing.T) {
 
 func Test_quotedPrintEncode(t *testing.T) {
 	var buf bytes.Buffer
-	text := "Dear reader!\n\n" +
+	text := []byte("Dear reader!\n\n" +
 		"This is a test email to try and capture some of the corner cases that exist within\n" +
 		"the quoted-printable encoding.\n" +
 		"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
-		"it can come out a little weird.  Also, we need to support unicode so here's a fish: 🐟\n"
-	expected := "Dear reader!\r\n\r\n" +
+		"it can come out a little weird.  Also, we need to support unicode so here's a fish: 🐟\n")
+	expected := []byte("Dear reader!\r\n\r\n" +
 		"This is a test email to try and capture some of the corner cases that exist=\r\n" +
 		" within\r\n" +
 		"the quoted-printable encoding.\r\n" +
 		"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
 		"s so=0D\r\n" +
 		"it can come out a little weird.  Also, we need to support unicode so here's=\r\n" +
-		" a fish: =F0=9F=90=9F\r\n"
+		" a fish: =F0=9F=90=9F\r\n")
 
 	if err := quotePrintEncode(&buf, text); err != nil {
 		t.Fatal("quotePrintEncode: ", err)
 	}
 
-	if s := buf.String(); s != expected {
-		t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", s, expected)
+	if b := buf.Bytes(); !bytes.Equal(b, expected) {
+		t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
 	}
 }
 
 func Benchmark_quotedPrintEncode(b *testing.B) {
-	text := "Dear reader!\n\n" +
+	text := []byte("Dear reader!\n\n" +
 		"This is a test email to try and capture some of the corner cases that exist within\n" +
 		"the quoted-printable encoding.\n" +
 		"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
-		"it can come out a little weird.  Also, we need to support unicode so here's a fish: 🐟\n"
+		"it can come out a little weird.  Also, we need to support unicode so here's a fish: 🐟\n")
 
 	for i := 0; i <= b.N; i++ {
 		if err := quotePrintEncode(ioutil.Discard, text); err != nil {