-
Notifications
You must be signed in to change notification settings - Fork 77
/
email.go
49 lines (46 loc) · 1.36 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
49
package main
import (
"log"
"net/smtp"
"strconv"
"strings"
)
func sendCodeByEmail(code int32, identity string, cfg Config) {
dest := []string{identity}
target := strings.Join(dest, ",")
subject := "Access Code"
bodyMessage := "Access code is " + strconv.Itoa(int((code)))
msg := "From: " + cfg.SMTP.Sender + "\n" +
"To: " + target + "\n" +
"Subject: " + subject + "\n" +
bodyMessage
auth := smtp.PlainAuth("", cfg.SMTP.User, cfg.SMTP.Pass, cfg.SMTP.Server)
err := smtp.SendMail(cfg.SMTP.Server+":"+cfg.SMTP.Port,
auth, cfg.SMTP.User, dest, []byte(msg))
log.Printf("Send email to %s via %s", target, cfg.SMTP.Server)
if err != nil {
log.Printf("Error sending email: %s", err)
return
}
log.Printf("Mail sent successfully!")
}
func adminEmailAlert(action string, adminEmail string, cfg Config) {
if len(adminEmail) == 0 {
return
}
dest := []string{adminEmail}
subject := "Data Subject request received"
bodyMessage := "Request: " + action
msg := "From: " + cfg.SMTP.Sender + "\n" +
"To: " + strings.Join(dest, ",") + "\n" +
"Subject: " + subject + "\n" +
bodyMessage
auth := smtp.PlainAuth("", cfg.SMTP.User, cfg.SMTP.Pass, cfg.SMTP.Server)
err := smtp.SendMail(cfg.SMTP.Server+":"+cfg.SMTP.Port,
auth, cfg.SMTP.User, dest, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
return
}
log.Println("Mail sent successfully!")
}