-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_email_utils.go
74 lines (62 loc) · 1.59 KB
/
notification_email_utils.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
package main
import (
"bytes"
"fmt"
"net/smtp"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
type Mail struct {
Sender string
To []string
Subject string
Body []byte
}
func getHostForSMTPAdress(address string) string {
strings.Split(address, ":")
host, _, found := strings.Cut(address, ":")
if !found {
cli.Exit("Invalid value format for flag --"+smtpSenderAddressFlag.Name, 1)
}
return host
}
func sendEmail(username string, password string, smtpServerAddress string, request *Mail, msg string) error {
auth := LoginAuth(username, password)
return smtp.SendMail(smtpServerAddress, auth, request.Sender, request.To, []byte(msg))
}
func buildMessage(mail Mail) string {
msg := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n"
msg += fmt.Sprintf("From: %s\r\n", mail.Sender)
msg += fmt.Sprintf("To: %s\r\n", strings.Join(mail.To, ";"))
msg += fmt.Sprintf("Subject: %s\r\n", mail.Subject)
msg += fmt.Sprintf("\r\n%s\r\n", mail.Body)
return msg
}
func addEmailContentOrDefault(filePathFlagValue string, defaultText string, emailBodyContent *bytes.Buffer) {
if filePathFlagValue != "" {
fileContent, err := os.ReadFile(filePathFlagValue)
if err != nil {
log.Error("Error reading file configured in flag", err)
}
emailBodyContent.Write(fileContent)
} else {
emailBodyContent.WriteString(defaultText)
}
}
var emailHTMLHeader = `<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
`