-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.go
48 lines (42 loc) · 890 Bytes
/
mailer.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 go_notifier_core
import (
"encoding/json"
"net/smtp"
)
type (
Mailer interface {
Send(fromName, fromMail, to, subject, message string) error
SetConfig(config []byte)
}
SmtpConfig struct {
Host string
Port string
Username string
Password string
Encryption string
}
SmtpMailer struct {
config *SmtpConfig
}
)
func (s *SmtpMailer) Send(fromName, fromMail, to, subject, message string) error {
auth := smtp.PlainAuth("", s.config.Username, s.config.Password, s.config.Host)
err := smtp.SendMail(
s.config.Host+":"+s.config.Port,
auth,
fromMail,
[]string{to},
[]byte("From: "+fromName+" <"+fromMail+">\r\n"+
"To: "+to+"\r\n"+
"Subject: "+subject+"\r\n"+
"\r\n"+
message+"\r\n"),
)
return err
}
func (s *SmtpMailer) SetConfig(config []byte) {
err := json.Unmarshal(config, &s.config)
if err != nil {
return
}
}