Skip to content

Commit

Permalink
Added an option to manually set filename of attachments
Browse files Browse the repository at this point in the history
Fixes #55
Fixes #56
  • Loading branch information
slavikm authored and alexcesaro committed Apr 1, 2016
1 parent bd0e445 commit 4291610
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 @@ -151,6 +151,10 @@ func ExampleSetHeader() {
m.Attach("foo.jpg", gomail.SetHeader(h))
}

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

func ExampleMessage_AddAlternative() {
m.SetBody("text/plain", "Hello!")
m.AddAlternative("text/html", "<p>Hello!</p>")
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 4291610

Please sign in to comment.