Skip to content

Commit b853423

Browse files
authored
Merge pull request #5 from tatang26/task-add-attachments
Attach files to email
2 parents ae76f13 + af09b0b commit b853423

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

sendgrid_sender.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sender
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"os"
@@ -53,11 +54,31 @@ func (ps SendgridSender) Send(m mail.Message) error {
5354
mm.AddPersonalizations(p)
5455
mm.AddContent(text, html)
5556

57+
for _, a := range m.Attachments {
58+
b := new(bytes.Buffer)
59+
if n, err := b.ReadFrom(a.Reader); err != nil {
60+
return fmt.Errorf("Error attaching file: n %v error %v", n, err)
61+
}
62+
63+
disposition := "attachment"
64+
if a.Embedded {
65+
disposition = "inline"
66+
}
67+
68+
attachment := smail.NewAttachment()
69+
attachment.SetFilename(a.Name)
70+
attachment.SetContentID(a.Name)
71+
attachment.SetContent(b.String())
72+
attachment.SetType(a.ContentType)
73+
attachment.SetDisposition(disposition)
74+
mm.AddAttachment(attachment)
75+
}
76+
5677
response, err := ps.client.Send(mm)
5778
if response.StatusCode != 202 {
5879
return fmt.Errorf("Error sending email, code %v body %v", response.StatusCode, response.Body)
5980
}
60-
81+
6182
return err
6283
}
6384

0 commit comments

Comments
 (0)