Skip to content

Commit

Permalink
Merge pull request #5 from tatang26/task-add-attachments
Browse files Browse the repository at this point in the history
Attach files to email
  • Loading branch information
paganotoni authored Jun 21, 2019
2 parents ae76f13 + af09b0b commit b853423
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sendgrid_sender.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sender

import (
"bytes"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -53,11 +54,31 @@ func (ps SendgridSender) Send(m mail.Message) error {
mm.AddPersonalizations(p)
mm.AddContent(text, html)

for _, a := range m.Attachments {
b := new(bytes.Buffer)
if n, err := b.ReadFrom(a.Reader); err != nil {
return fmt.Errorf("Error attaching file: n %v error %v", n, err)
}

disposition := "attachment"
if a.Embedded {
disposition = "inline"
}

attachment := smail.NewAttachment()
attachment.SetFilename(a.Name)
attachment.SetContentID(a.Name)
attachment.SetContent(b.String())
attachment.SetType(a.ContentType)
attachment.SetDisposition(disposition)
mm.AddAttachment(attachment)
}

response, err := ps.client.Send(mm)
if response.StatusCode != 202 {
return fmt.Errorf("Error sending email, code %v body %v", response.StatusCode, response.Body)
}

return err
}

Expand Down

0 comments on commit b853423

Please sign in to comment.