Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mail attachment name garbled #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many maintainers dislike the addition of editor-specific ignores. Please use a global .gitignore to ignore your editor's project files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I fixed it. But this project is under maintenance yet?

22 changes: 11 additions & 11 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestAttachmentOnly(t *testing.T) {
to: []string{"[email protected]"},
content: "From: [email protected]\r\n" +
"To: [email protected]\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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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" +
Expand All @@ -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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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: <test-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: <image2.jpg>\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
Expand Down Expand Up @@ -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: <image.jpg>\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
Expand All @@ -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" +
Expand Down
9 changes: 5 additions & 4 deletions writeto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gomail
import (
"encoding/base64"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down