-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
email_service.go
52 lines (39 loc) · 1004 Bytes
/
email_service.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
package email_service
import (
"errors"
"github.com/op/go-logging"
"gopkg.in/gomail.v2"
)
var (
log = logging.MustGetLogger("email")
)
type EmailService struct {
cfg *EmailServiceConfig
}
func NewEmailService(cfg *EmailServiceConfig) (*EmailService, error) {
if cfg.Auth == "" || cfg.Pass == "" || cfg.Smtp == "" || cfg.Port == 0 ||
cfg.Sender == "" {
return nil, errors.New("bad parameters")
}
client := &EmailService{
cfg: cfg,
}
return client, nil
}
func (e EmailService) Send(email *Email) error {
email.From = e.cfg.Sender
m := gomail.NewMessage()
m.SetHeaders(map[string][]string{
"From": {email.From},
"Reply-To": {email.From},
"To": {email.To},
"Subject": {email.Subject},
})
m.SetBody("text/html", email.Body)
d := gomail.NewPlainDialer(e.cfg.Smtp, e.cfg.Port, e.cfg.Auth, e.cfg.Pass)
if err := d.DialAndSend(m); err != nil {
return errors.New(err.Error())
}
log.Debug("Sent email '" + email.Subject + "' to:" + email.To)
return nil
}