forked from matcornic/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.go
224 lines (196 loc) · 6.65 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
218
219
220
221
222
223
224
package hermes
import (
"bytes"
"html/template"
"github.com/Masterminds/sprig"
"github.com/imdario/mergo"
"github.com/jaytaylor/html2text"
"github.com/russross/blackfriday/v2"
"github.com/vanng822/go-premailer/premailer"
)
// Hermes is an instance of the hermes email generator
type Hermes struct {
Theme Theme
TextDirection TextDirection
Product Product
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)
},
}
// TDLeftToRight is the text direction from left to right (default)
const TDLeftToRight TextDirection = "ltr"
// TDRightToLeft is the text direction from right to left
const TDRightToLeft TextDirection = "rtl"
// Product represents your company product (brand)
// Appears in header & footer of e-mails
type Product struct {
Name string
Link string // e.g. https://matcornic.github.io
Logo string // e.g. https://matcornic.github.io/img/logo.png
Copyright string // Copyright © 2019 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 setDefaultEmailValues(e *Email) 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 setDefaultHermesValues(h *Hermes) error {
defaultTextDirection := TDLeftToRight
defaultHermes := Hermes{
Theme: new(Default),
TextDirection: defaultTextDirection,
Product: Product{
Name: "Hermes",
Copyright: "Copyright © 2020 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
}
if h.TextDirection != TDLeftToRight && h.TextDirection != TDRightToLeft {
h.TextDirection = defaultTextDirection
}
return nil
}
// GenerateHTML generates the email body from data to an HTML Reader
// This is for modern email clients
func (h *Hermes) GenerateHTML(email Email) (string, error) {
err := setDefaultHermesValues(h)
if err != nil {
return "", err
}
return h.generateTemplate(email, h.Theme.HTMLTemplate())
}
// GeneratePlainText generates the email body from data
// This is for old email clients
func (h *Hermes) GeneratePlainText(email Email) (string, error) {
err := setDefaultHermesValues(h)
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 := setDefaultEmailValues(&email)
if err != nil {
return "", err
}
// Generate the email from Golang template
// Allow usage of simple function from sprig : https://github.com/Masterminds/sprig
t, err := template.New("hermes").Funcs(sprig.FuncMap()).Funcs(templateFuncs).Funcs(template.FuncMap{
"safe": func(s string) template.HTML { return template.HTML(s) }, // Used for keeping comments in generated template
}).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
}