diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ + diff --git a/message_test.go b/message_test.go index acceff2..cebf781 100644 --- a/message_test.go +++ b/message_test.go @@ -248,7 +248,7 @@ func TestAttachmentOnly(t *testing.T) { to: []string{"to@example.com"}, content: "From: from@example.com\r\n" + "To: to@example.com\r\n" + - "Content-Type: application/pdf; name=\"test.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"test.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + @@ -279,7 +279,7 @@ func TestAttachment(t *testing.T) { "\r\n" + "Test\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/pdf; name=\"test.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"test.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + @@ -313,7 +313,7 @@ func TestRename(t *testing.T) { "\r\n" + "Test\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/pdf; name=\"another.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"another.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"another.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + @@ -340,13 +340,13 @@ func TestAttachmentsOnly(t *testing.T) { " boundary=_BOUNDARY_1_\r\n" + "\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/pdf; name=\"test.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"test.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/zip; name=\"test.zip\"\r\n" + + "Content-Type: application/zip; charset=UTF-8; name=\"test.zip\"\r\n" + "Content-Disposition: attachment; filename=\"test.zip\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + @@ -379,13 +379,13 @@ func TestAttachments(t *testing.T) { "\r\n" + "Test\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/pdf; name=\"test.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"test.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/zip; name=\"test.zip\"\r\n" + + "Content-Type: application/zip; charset=UTF-8; name=\"test.zip\"\r\n" + "Content-Disposition: attachment; filename=\"test.zip\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + @@ -418,14 +418,14 @@ func TestEmbedded(t *testing.T) { "\r\n" + "Test\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: image/jpeg; name=\"image1.jpg\"\r\n" + + "Content-Type: image/jpeg; charset=UTF-8; name=\"image1.jpg\"\r\n" + "Content-Disposition: inline; filename=\"image1.jpg\"\r\n" + "Content-ID: \r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + base64.StdEncoding.EncodeToString([]byte("Content of image1.jpg")) + "\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: image/jpeg; name=\"image2.jpg\"\r\n" + + "Content-Type: image/jpeg; charset=UTF-8; name=\"image2.jpg\"\r\n" + "Content-Disposition: inline; filename=\"image2.jpg\"\r\n" + "Content-ID: \r\n" + "Content-Transfer-Encoding: base64\r\n" + @@ -475,7 +475,7 @@ func TestFullMessage(t *testing.T) { "--_BOUNDARY_3_--\r\n" + "\r\n" + "--_BOUNDARY_2_\r\n" + - "Content-Type: image/jpeg; name=\"image.jpg\"\r\n" + + "Content-Type: image/jpeg; charset=UTF-8; name=\"image.jpg\"\r\n" + "Content-Disposition: inline; filename=\"image.jpg\"\r\n" + "Content-ID: \r\n" + "Content-Transfer-Encoding: base64\r\n" + @@ -484,7 +484,7 @@ func TestFullMessage(t *testing.T) { "--_BOUNDARY_2_--\r\n" + "\r\n" + "--_BOUNDARY_1_\r\n" + - "Content-Type: application/pdf; name=\"test.pdf\"\r\n" + + "Content-Type: application/pdf; charset=UTF-8; name=\"test.pdf\"\r\n" + "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + diff --git a/writeto.go b/writeto.go index 9fb6b86..224621f 100644 --- a/writeto.go +++ b/writeto.go @@ -3,6 +3,7 @@ package gomail import ( "encoding/base64" "errors" + "fmt" "io" "mime" "mime/multipart" @@ -45,12 +46,12 @@ func (w *messageWriter) writeMessage(m *Message) { w.closeMultipart() } - w.addFiles(m.embedded, false) + w.addFiles(m.embedded, false, m.charset) if m.hasRelatedPart() { w.closeMultipart() } - w.addFiles(m.attachments, true) + w.addFiles(m.attachments, true, m.charset) if m.hasMixedPart() { w.closeMultipart() } @@ -112,14 +113,14 @@ func (w *messageWriter) writePart(p *part, charset string) { w.writeBody(p.copier, p.encoding) } -func (w *messageWriter) addFiles(files []*file, isAttachment bool) { +func (w *messageWriter) addFiles(files []*file, isAttachment bool, charset string) { for _, f := range files { if _, ok := f.Header["Content-Type"]; !ok { mediaType := mime.TypeByExtension(filepath.Ext(f.Name)) if mediaType == "" { mediaType = "application/octet-stream" } - f.setHeader("Content-Type", mediaType+`; name="`+f.Name+`"`) + f.setHeader("Content-Type", fmt.Sprintf(`%s; charset=%s; name="%s"`, mediaType, charset, f.Name)) } if _, ok := f.Header["Content-Transfer-Encoding"]; !ok {