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

Accept io.Reader for Attach and Embed #78

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
49 changes: 37 additions & 12 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,28 @@ func SetCopyFunc(f func(io.Writer) error) FileSetting {
}
}

func (m *Message) appendFile(list []*file, name string, settings []FileSetting) []*file {
f := &file{
// AttachReader attaches a file using an io.Reader
func (m *Message) AttachReader(name string, r io.Reader, settings ...FileSetting) {
m.attachments = m.appendFile(m.attachments, fileFromReader(name, r), settings)
}

// Attach attaches the files to the email.
func (m *Message) Attach(filename string, settings ...FileSetting) {
m.attachments = m.appendFile(m.attachments, fileFromFilename(filename), settings)
}

// EmbedReader embeds the images to the email.
func (m *Message) EmbedReader(name string, r io.Reader, settings ...FileSetting) {
m.embedded = m.appendFile(m.embedded, fileFromReader(name, r), settings)
}

// Embed embeds the images to the email.
func (m *Message) Embed(filename string, settings ...FileSetting) {
m.embedded = m.appendFile(m.embedded, fileFromFilename(filename), settings)
}

func fileFromFilename(name string) *file {
return &file{
Name: filepath.Base(name),
Header: make(map[string][]string),
CopyFunc: func(w io.Writer) error {
Expand All @@ -299,7 +319,22 @@ func (m *Message) appendFile(list []*file, name string, settings []FileSetting)
return h.Close()
},
}
}

func fileFromReader(name string, r io.Reader) *file {
return &file{
Name: filepath.Base(name),
Header: make(map[string][]string),
CopyFunc: func(w io.Writer) error {
if _, err := io.Copy(w, r); err != nil {
return err
}
return nil
},
}
}

func (m *Message) appendFile(list []*file, f *file, settings []FileSetting) []*file {
for _, s := range settings {
s(f)
}
Expand All @@ -310,13 +345,3 @@ func (m *Message) appendFile(list []*file, name string, settings []FileSetting)

return append(list, f)
}

// Attach attaches the files to the email.
func (m *Message) Attach(filename string, settings ...FileSetting) {
m.attachments = m.appendFile(m.attachments, filename, settings)
}

// Embed embeds the images to the email.
func (m *Message) Embed(filename string, settings ...FileSetting) {
m.embedded = m.appendFile(m.embedded, filename, settings)
}
49 changes: 49 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,30 @@ func TestBodyWriter(t *testing.T) {
testMessage(t, m, 1, want)
}

func TestAttachmentReader(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")

var b bytes.Buffer
b.Write([]byte("Test file"))
m.AttachReader("file.txt", &b)

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: text/plain; charset=utf-8; name=\"file.txt\"\r\n" +
"Content-Disposition: attachment; filename=\"file.txt\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
base64.StdEncoding.EncodeToString([]byte("Test file")),
}

testMessage(t, m, 0, want)
}

func TestAttachmentOnly(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "from@example.com")
Expand Down Expand Up @@ -396,6 +420,31 @@ func TestAttachments(t *testing.T) {
testMessage(t, m, 1, want)
}

func TestEmbeddedReader(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")

var b bytes.Buffer
b.Write([]byte("Test file"))
m.EmbedReader("file.txt", &b)

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: text/plain; charset=utf-8; name=\"file.txt\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"Content-Disposition: inline; filename=\"file.txt\"\r\n" +
"Content-ID: <file.txt>\r\n" +
"\r\n" +
base64.StdEncoding.EncodeToString([]byte("Test file")),
}

testMessage(t, m, 0, want)
}

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