-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (118 loc) · 3.81 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/smtp"
"os"
)
// EmailConfig struct holds every data required to authenticate on an SMTP Server and also identifies the sender's name and email
type EmailConfig struct {
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Port string `json:"port"`
SenderName string `json:"sender-name"`
SenderEmail string `json:"sender-email"`
}
// EmailVariables struct holds every variable name and its corresponding value it's meant to be replaced on every email body sent.
type EmailVariables struct {
Variables []map[string]interface{} `json:"variables"`
}
func main() {
emailConfigData, err := openAndReadFile("email-config.json")
if err != nil {
printErrorAndDie("Error reading email configuration: ", err)
}
emailVariablesData, err := openAndReadFile("email-data.json")
if err != nil {
printErrorAndDie("Error reading variable configuration: ", err)
}
emailConfig, err := initEmailConfig(emailConfigData)
if err != nil {
printErrorAndDie("Error initiating email configuration: ", err)
}
emailVariables, err := initEmailVariables(emailVariablesData)
if err != nil {
printErrorAndDie("Error initiating variable configuration: ", err)
}
smtpAuth := configEmailAuth(emailConfig)
err = emailVariables.sendEmails(emailConfig, smtpAuth)
if err != nil {
printErrorAndDie("Error sending email: ", err)
}
}
func printErrorAndDie(description string, err error) {
fmt.Println(description + err.Error())
os.Exit(1)
}
func openAndReadFile(filename string) ([]byte, error) {
jsonFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return jsonFile, nil
}
func initEmailConfig(fileData []byte) (EmailConfig, error) {
var emailConfig EmailConfig
if err := json.Unmarshal(fileData, &emailConfig); err != nil {
return EmailConfig{}, err
}
return emailConfig, nil
}
func initEmailVariables(fileData []byte) (EmailVariables, error) {
var emailVariables EmailVariables
if err := json.Unmarshal(fileData, &emailVariables); err != nil {
return EmailVariables{}, err
}
return emailVariables, nil
}
func configEmailAuth(emailConfig EmailConfig) smtp.Auth {
return smtp.PlainAuth("", emailConfig.User, emailConfig.Password, emailConfig.Host)
}
func configureEmailVariables(emailConfig EmailConfig, k int, emailVariables EmailVariables) map[string]interface{} {
vars := make(map[string]interface{})
vars["SenderName"] = emailConfig.SenderName
vars["SenderEmail"] = emailConfig.SenderEmail
for _k := range emailVariables.Variables[k] {
if str, ok := emailVariables.Variables[k][_k].(string); ok {
vars[_k] = str
}
}
return vars
}
func configureEmailTemplate(vars map[string]interface{}) (string, error) {
template, err := template.New("email.tmpl").Funcs(template.FuncMap{
"emailAddressStructure": func(str string) template.HTML {
return template.HTML(fmt.Sprintf("<%s>", str))
},
}).ParseFiles("email.tmpl")
if err != nil {
return "", err
}
var emailBytes bytes.Buffer
err = template.Execute(&emailBytes, vars)
if err != nil {
return "", err
}
return emailBytes.String(), nil
}
func (emailVariables *EmailVariables) sendEmails(emailConfig EmailConfig, smtpAuth smtp.Auth) error {
for k := range emailVariables.Variables {
vars := configureEmailVariables(emailConfig, k, *emailVariables)
emailTemplate, err := configureEmailTemplate(vars)
if err != nil {
return err
}
fmt.Println("Sending email to: " + vars["RecipientEmail"].(string) + "...")
if err := smtp.SendMail(fmt.Sprintf("%s:%s", emailConfig.Host, emailConfig.Port), smtpAuth, emailConfig.User, []string{vars["RecipientEmail"].(string)}, []byte(emailTemplate)); err != nil {
return err
}
fmt.Println("Email sent!")
fmt.Println()
vars = nil
}
return nil
}