forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp_sender.go
91 lines (70 loc) · 1.94 KB
/
smtp_sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mail
import (
"io"
"strconv"
"github.com/pkg/errors"
gomail "gopkg.in/mail.v2"
)
//SMTPSender allows to send Emails by connecting to a SMTP server.
type SMTPSender struct {
Dialer *gomail.Dialer
}
//Send a message using SMTP configuration or returns an error if something goes wrong.
func (sm SMTPSender) Send(message Message) error {
gm := gomail.NewMessage()
gm.SetHeader("From", message.From)
gm.SetHeader("To", message.To...)
gm.SetHeader("Subject", message.Subject)
gm.SetHeader("Cc", message.CC...)
gm.SetHeader("Bcc", message.Bcc...)
sm.addBodies(message, gm)
sm.addAttachments(message, gm)
for field, value := range message.Headers {
gm.SetHeader(field, value)
}
err := sm.Dialer.DialAndSend(gm)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (sm SMTPSender) addBodies(message Message, gm *gomail.Message) {
if len(message.Bodies) == 0 {
return
}
mainBody := message.Bodies[0]
gm.SetBody(mainBody.ContentType, mainBody.Content, gomail.SetPartEncoding(gomail.Unencoded))
for i := 1; i < len(message.Bodies); i++ {
alt := message.Bodies[i]
gm.AddAlternative(alt.ContentType, alt.Content, gomail.SetPartEncoding(gomail.Unencoded))
}
}
func (sm SMTPSender) addAttachments(message Message, gm *gomail.Message) {
for _, at := range message.Attachments {
settings := gomail.SetCopyFunc(func(w io.Writer) error {
if _, err := io.Copy(w, at.Reader); err != nil {
return err
}
return nil
})
gm.Attach(at.Name, settings)
}
}
//NewSMTPSender builds a SMTP mail based in passed config.
func NewSMTPSender(host string, port string, user string, password string) (SMTPSender, error) {
iport, err := strconv.Atoi(port)
if err != nil {
return SMTPSender{}, errors.New("invalid port for the SMTP mail")
}
dialer := &gomail.Dialer{
Host: host,
Port: iport,
}
if user != "" {
dialer.Username = user
dialer.Password = password
}
return SMTPSender{
Dialer: dialer,
}, nil
}