Skip to content

Commit

Permalink
Added an option to manually set filename of attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
slavikm committed Mar 31, 2016
1 parent bd0e445 commit 9e773b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func ExampleMessage_Attach() {
m.Attach("/tmp/image.jpg")
}

func ExampleMessage_AttachAndRename() {
m.Attach("/tmp/image.jpg", gomail.Rename("picture.jpg"))
}

func ExampleMessage_Embed() {
m.Embed("/tmp/image.jpg")
m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
Expand Down
8 changes: 8 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ func SetHeader(h map[string][]string) FileSetting {
}
}

// Rename is a file setting to set the name of the attachment if the name is
// different than the filename on disk.
func Rename(name string) FileSetting {
return func(f *file) {
f.Name = name
}
}

// SetCopyFunc is a file setting to replace the function that runs when the
// message is sent. It should copy the content of the file to the io.Writer.
//
Expand Down
34 changes: 34 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,40 @@ func TestAttachment(t *testing.T) {
testMessage(t, m, 1, want)
}

func TestRename(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetBody("text/plain", "Test")
name, copy := mockCopyFile("/tmp/test.pdf")
rename := Rename("another.pdf")
m.Attach(name, copy, rename)

want := &message{
from: "from@example.com",
to: []string{"to@example.com"},
content: "From: from@example.com\r\n" +
"To: to@example.com\r\n" +
"Content-Type: multipart/mixed;\r\n" +
" boundary=_BOUNDARY_1_\r\n" +
"\r\n" +
"--_BOUNDARY_1_\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"\r\n" +
"Test\r\n" +
"--_BOUNDARY_1_\r\n" +
"Content-Type: application/pdf; name=\"another.pdf\"\r\n" +
"Content-Disposition: attachment; filename=\"another.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",
}

testMessage(t, m, 1, want)
}

func TestAttachmentsOnly(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "from@example.com")
Expand Down

0 comments on commit 9e773b6

Please sign in to comment.