forked from Laisky/go-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.go
More file actions
64 lines (55 loc) · 1.53 KB
/
email.go
File metadata and controls
64 lines (55 loc) · 1.53 KB
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
package utils
import (
zap "github.com/Laisky/zap"
"github.com/pkg/errors"
gomail "gopkg.in/gomail.v2"
)
// Mail easy way to send basic email
type Mail struct {
host string
port int
username, password string
}
// NewMail create Mail with SMTP host and port
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,
}
}
// Login login to SMTP server
func (m *Mail) Login(username, password string) {
Logger.Debug("login", zap.String("username", username))
m.username = username
m.password = password
}
// BuildMessage implement
func (m *Mail) BuildMessage(msg string) string {
return msg
}
// Send send email
func (m *Mail) Send(frAddr, toAddr, frName, toName, subject, content string) (err error) {
Logger.Info("send email", zap.String("toName", toName))
s := gomail.NewMessage()
s.SetAddressHeader("From", frAddr, frName)
s.SetAddressHeader("To", toAddr, toName)
s.SetHeader("Subject", subject)
s.SetBody("text/plain", content)
d := gomail.NewDialer(m.host, m.port, m.username, m.password)
if Settings.GetBool("dry") {
Logger.Info("try to send email",
zap.String("fromAddr", frAddr),
zap.String("toAddr", toAddr),
zap.String("frName", frName),
zap.String("toName", toName),
zap.String("subject", subject),
zap.String("content", content),
)
} else {
if err := d.DialAndSend(s); err != nil {
return errors.Wrap(err, "try to send email got error")
}
}
return nil
}