forked from Laisky/go-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
48 lines (39 loc) · 1.08 KB
/
email.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
package utils
import (
"github.com/pkg/errors"
zap "github.com/Laisky/zap"
gomail "gopkg.in/gomail.v2"
)
type Mail struct {
host string
port int
username, password string
}
func NewMail(host string, port int) *Mail {
Logger.Debug("try to send mail", zap.String("host", host), zap.Int("port", port))
return &Mail{
host: host,
port: port,
}
}
func (m *Mail) Login(username, password string) {
Logger.Debug("login", zap.String("username", username))
m.username = username
m.password = password
}
func (m *Mail) BuildMessage(msg string) string {
return msg
}
func (m *Mail) Send(fr, to, frName, toName, subject, content string) (err error) {
Logger.Info("send email", zap.String("toName", toName))
s := gomail.NewMessage()
s.SetAddressHeader("From", fr, frName)
s.SetAddressHeader("To", to, toName)
s.SetHeader("Subject", subject)
s.SetBody("text/plain", content)
d := gomail.NewPlainDialer(m.host, m.port, m.username, m.password)
if err := d.DialAndSend(s); err != nil {
return errors.Wrap(err, "try to send email got error")
}
return nil
}