-
Notifications
You must be signed in to change notification settings - Fork 240
/
main.go
178 lines (155 loc) · 4.17 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"fmt"
"github.com/go-gomail/gomail"
"github.com/matcornic/hermes/v2"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"net/mail"
"os"
"strconv"
)
type example interface {
Email() hermes.Email
Name() string
}
func main() {
h := hermes.Hermes{
Product: hermes.Product{
Name: "Hermes",
Link: "https://example-hermes.com/",
Logo: "https://github.com/matcornic/hermes/blob/master/examples/gopher.png?raw=true",
},
}
sendEmails := os.Getenv("HERMES_SEND_EMAILS") == "true"
examples := []example{
new(welcome),
new(reset),
new(receipt),
new(maintenance),
new(inviteCode),
}
themes := []hermes.Theme{
new(hermes.Default),
new(hermes.Flat),
}
// Generate emails
for _, theme := range themes {
h.Theme = theme
for _, e := range examples {
generateEmails(h, e.Email(), e.Name())
}
}
// Send emails only when requested
if sendEmails {
port, _ := strconv.Atoi(os.Getenv("HERMES_SMTP_PORT"))
password := os.Getenv("HERMES_SMTP_PASSWORD")
SMTPUser := os.Getenv("HERMES_SMTP_USER")
if password == "" {
fmt.Printf("Enter SMTP password of '%s' account: ", SMTPUser)
bytePassword, _ := terminal.ReadPassword(0)
password = string(bytePassword)
}
smtpConfig := smtpAuthentication{
Server: os.Getenv("HERMES_SMTP_SERVER"),
Port: port,
SenderEmail: os.Getenv("HERMES_SENDER_EMAIL"),
SenderIdentity: os.Getenv("HERMES_SENDER_IDENTITY"),
SMTPPassword: password,
SMTPUser: SMTPUser,
}
options := sendOptions{
To: os.Getenv("HERMES_TO"),
}
for _, theme := range themes {
h.Theme = theme
for _, e := range examples {
options.Subject = "Hermes | " + h.Theme.Name() + " | " + e.Name()
fmt.Printf("Sending email '%s'...\n", options.Subject)
htmlBytes, err := ioutil.ReadFile(fmt.Sprintf("%v/%v.%v.html", h.Theme.Name(), h.Theme.Name(), e.Name()))
if err != nil {
panic(err)
}
txtBytes, err := ioutil.ReadFile(fmt.Sprintf("%v/%v.%v.txt", h.Theme.Name(), h.Theme.Name(), e.Name()))
if err != nil {
panic(err)
}
err = send(smtpConfig, options, string(htmlBytes), string(txtBytes))
if err != nil {
panic(err)
}
}
}
}
}
func generateEmails(h hermes.Hermes, email hermes.Email, example string) {
// Generate the HTML template and save it
res, err := h.GenerateHTML(email)
if err != nil {
panic(err)
}
err = os.MkdirAll(h.Theme.Name(), 0744)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%v/%v.%v.html", h.Theme.Name(), h.Theme.Name(), example), []byte(res), 0644)
if err != nil {
panic(err)
}
// Generate the plaintext template and save it
res, err = h.GeneratePlainText(email)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%v/%v.%v.txt", h.Theme.Name(), h.Theme.Name(), example), []byte(res), 0644)
if err != nil {
panic(err)
}
}
type smtpAuthentication struct {
Server string
Port int
SenderEmail string
SenderIdentity string
SMTPUser string
SMTPPassword string
}
// sendOptions are options for sending an email
type sendOptions struct {
To string
Subject string
}
// send sends the email
func send(smtpConfig smtpAuthentication, options sendOptions, htmlBody string, txtBody string) error {
if smtpConfig.Server == "" {
return errors.New("SMTP server config is empty")
}
if smtpConfig.Port == 0 {
return errors.New("SMTP port config is empty")
}
if smtpConfig.SMTPUser == "" {
return errors.New("SMTP user is empty")
}
if smtpConfig.SenderIdentity == "" {
return errors.New("SMTP sender identity is empty")
}
if smtpConfig.SenderEmail == "" {
return errors.New("SMTP sender email is empty")
}
if options.To == "" {
return errors.New("no receiver emails configured")
}
from := mail.Address{
Name: smtpConfig.SenderIdentity,
Address: smtpConfig.SenderEmail,
}
m := gomail.NewMessage()
m.SetHeader("From", from.String())
m.SetHeader("To", options.To)
m.SetHeader("Subject", options.Subject)
m.SetBody("text/plain", txtBody)
m.AddAlternative("text/html", htmlBody)
d := gomail.NewDialer(smtpConfig.Server, smtpConfig.Port, smtpConfig.SMTPUser, smtpConfig.SMTPPassword)
return d.DialAndSend(m)
}