-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.go
217 lines (188 loc) · 6.13 KB
/
hermes.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package hermes
import (
"bytes"
"html/template"
"github.com/Masterminds/sprig/v3"
"github.com/imdario/mergo"
"github.com/jaytaylor/html2text"
"github.com/russross/blackfriday/v2"
"github.com/unknowns24/hermes/pkg/themes"
"github.com/vanng822/go-premailer/premailer"
)
// Hermes is an instance of the hermes email generator
type Hermes struct {
Theme Theme
Brand Branding
TextDirection TextDirection
DisableCSSInlining bool
}
// Theme is an interface to implement when creating a new theme
type Theme interface {
Name() string // The name of the theme
HTMLTemplate() string // The golang template for HTML emails
PlainTextTemplate() string // The golang templte for plain text emails (can be basic HTML)
}
// TextDirection of the text in HTML email
type TextDirection string
var templateFuncs = template.FuncMap{
"url": func(s string) template.URL {
return template.URL(s)
},
}
// Appears in header & footer of e-mails
type Branding struct {
Name string
Link string // e.g. https://google.com
Logo string // e.g. https://google.com/img/logo.png
Copyright string // Copyright © 2024 Hermes. All rights reserved.
TroubleText string // TroubleText is the sentence at the end of the email for users having trouble with the button (default to `If you’re having trouble with the button '{ACTION}', copy and paste the URL below into your web browser.`)
}
// Email is the email containing a body
type Email struct {
Body Body
}
// Markdown is a HTML template (a string) representing Markdown content
// https://en.wikipedia.org/wiki/Markdown
type Markdown template.HTML
// Body is the body of the email, containing all interesting data
type Body struct {
Name string // The name of the contacted person
Intros []string // Intro sentences, first displayed in the email
Dictionary []Entry // A list of key+value (useful for displaying parameters/settings/personal info)
Table Table // Table is an table where you can put data (pricing grid, a bill, and so on)
Actions []Action // Actions are a list of actions that the user will be able to execute via a button click
Outros []string // Outro sentences, last displayed in the email
Greeting string // Greeting for the contacted person (default to 'Hi')
Signature string // Signature for the contacted person (default to 'Yours truly')
Title string // Title replaces the greeting+name when set
FreeMarkdown Markdown // Free markdown content that replaces all content other than header and footer
}
// ToHTML converts Markdown to HTML
func (c Markdown) ToHTML() template.HTML {
return template.HTML(blackfriday.Run([]byte(string(c))))
}
// Entry is a simple entry of a map
// Allows using a slice of entries instead of a map
// Because Golang maps are not ordered
type Entry struct {
Key string
Value string
}
// Table is an table where you can put data (pricing grid, a bill, and so on)
type Table struct {
Data [][]Entry // Contains data
Columns Columns // Contains meta-data for display purpose (width, alignement)
}
// Columns contains meta-data for the different columns
type Columns struct {
CustomWidth map[string]string
CustomAlignment map[string]string
}
// Action is anything the user can act on (i.e., click on a button, view an invite code)
type Action struct {
Instructions string
Button Button
InviteCode string
}
// Button defines an action to launch
type Button struct {
Color string
TextColor string
Text string
Link string
}
// Template is the struct given to Golang templating
// Root object in a template is this struct
type Template struct {
Hermes Hermes
Email Email
}
func (e *Email) SetDefaultEmailValues() error {
// Default values of an email
defaultEmail := Email{
Body: Body{
Intros: []string{},
Dictionary: []Entry{},
Outros: []string{},
Signature: "Yours truly",
Greeting: "Hi",
},
}
// Merge the given email with default one
// Default one overrides all zero values
return mergo.Merge(e, defaultEmail)
}
// default values of the engine
func (h *Hermes) SetDefaultHermesValues() error {
defaultHermes := Hermes{
Theme: new(themes.Default),
TextDirection: "ltr",
Brand: Branding{
Name: "Hermes",
Copyright: "Copyright © 2024 Hermes. All rights reserved.",
TroubleText: "If you’re having trouble with the button '{ACTION}', copy and paste the URL below into your web browser.",
},
}
// Merge the given hermes engine configuration with default one
// Default one overrides all zero values
err := mergo.Merge(h, defaultHermes)
if err != nil {
return err
}
return nil
}
// GenerateHTML genera el cuerpo del correo electrónico en formato HTML para clientes modernos.
func (h *Hermes) GenerateHTML(email Email) (string, error) {
err := h.SetDefaultHermesValues()
if err != nil {
return "", err
}
return h.generateTemplate(email, h.Theme.HTMLTemplate())
}
// GeneratePlainText genera el cuerpo del correo electrónico en formato de texto sin formato para clientes antiguos.
func (h *Hermes) GeneratePlainText(email Email) (string, error) {
err := h.SetDefaultHermesValues()
if err != nil {
return "", err
}
template, err := h.generateTemplate(email, h.Theme.PlainTextTemplate())
if err != nil {
return "", err
}
return html2text.FromString(template, html2text.Options{PrettyTables: true})
}
func (h *Hermes) generateTemplate(email Email, tplt string) (string, error) {
err := email.SetDefaultEmailValues()
if err != nil {
return "", err
}
t, err := template.New("hermes").
Funcs(sprig.FuncMap()).
Funcs(templateFuncs).
Funcs(template.FuncMap{
"safe": func(s string) template.HTML { return template.HTML(s) },
}).
Parse(tplt)
if err != nil {
return "", err
}
var b bytes.Buffer
err = t.Execute(&b, Template{*h, email})
if err != nil {
return "", err
}
res := b.String()
if h.DisableCSSInlining {
return res, nil
}
// Inlining CSS
prem, err := premailer.NewPremailerFromString(res, premailer.NewOptions())
if err != nil {
return "", err
}
html, err := prem.Transform()
if err != nil {
return "", err
}
return html, nil
}