-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemails.go
78 lines (65 loc) · 2.32 KB
/
emails.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/smtp"
)
type Settings struct {
SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"`
SMTPHostUser string `json:"smtp_host_user"`
SMTPHostPassword string `json:"smtp_host_password"`
SMTPFromEmail string `json:"smtp_from_email"`
EmailRecipients []string `json:"email_alert_recipients"`
}
func TriggerEmail(subject string, body string) {
url := BaseURL + "/core/settings/"
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("X-API-KEY", ApiKey)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var settings Settings
if err := json.NewDecoder(resp.Body).Decode(&settings); err != nil {
panic(err)
}
// Check if all SMTP settings are provided
if settings.SMTPHost == "" || settings.SMTPPort == 0 ||
settings.SMTPHostUser == "" || settings.SMTPHostPassword == "" ||
settings.SMTPFromEmail == "" || len(settings.EmailRecipients) == 0 {
fmt.Println("SMTP configuration not complete, skipping email alert.")
return
}
SendEmail(settings, subject, body)
}
func SendEmail(settings Settings, subject string, body string) {
from := settings.SMTPFromEmail
header := make(map[string]string)
header["From"] = from
header["Subject"] = subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=\"utf-8\""
serverName := fmt.Sprintf("%s:%d", settings.SMTPHost, settings.SMTPPort)
auth := smtp.PlainAuth("", settings.SMTPHostUser, settings.SMTPHostPassword, settings.SMTPHost)
for _, to := range settings.EmailRecipients {
header["To"] = to
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + body
err := smtp.SendMail(serverName, auth, from, []string{to}, []byte(message))
if err != nil {
fmt.Printf("Failed to send email to %s: %v\n", to, err)
} else {
fmt.Printf("Email sent successfully to %s\n", to)
}
}
}