forked from matcornic/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.go
196 lines (168 loc) · 5.81 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
package hermes
import (
"bytes"
"html/template"
"github.com/Masterminds/sprig/v3"
"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(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) {
if e.Body.Intros == nil {
e.Body.Intros = []string{}
}
if e.Body.Dictionary == nil {
e.Body.Dictionary = []Entry{}
}
if e.Body.Outros == nil {
e.Body.Outros = []string{}
}
}
// default values of the engine
func setDefaultHermesValues(h *Hermes) {
if h.Theme == nil {
h.Theme = new(Default)
}
if h.TextDirection != TDLeftToRight && h.TextDirection != TDRightToLeft {
h.TextDirection = TDLeftToRight
}
}
// 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) {
setDefaultHermesValues(h)
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) {
setDefaultHermesValues(h)
tmpl, err := h.generateTemplate(email, h.Theme.PlainTextTemplate())
if err != nil {
return "", err
}
return html2text.FromString(tmpl, html2text.Options{PrettyTables: true})
}
func (h *Hermes) generateTemplate(email Email, tplt string) (string, error) {
setDefaultEmailValues(&email)
// 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
}