From cbc4728f96701e2019130d331c0120cd6f50681b Mon Sep 17 00:00:00 2001 From: LYY Date: Thu, 20 Apr 2017 09:36:37 +0800 Subject: [PATCH 1/3] fix mail attachment name garbled --- .gitignore | 1 + writeto.go | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode 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 { From 39ec8615f111157ac1e006b5a211ba026e5dd80c Mon Sep 17 00:00:00 2001 From: LYY Date: Thu, 20 Apr 2017 10:17:11 +0800 Subject: [PATCH 2/3] fix message test --- message_test.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) 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" + From 90adcb3c5cbb816cec5e2433918d9458f74e9914 Mon Sep 17 00:00:00 2001 From: Liu Yuyu Date: Sat, 7 Oct 2017 17:31:39 +0800 Subject: [PATCH 3/3] remove .vscode ignores --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 722d5e7..8b13789 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.vscode +