-
Notifications
You must be signed in to change notification settings - Fork 1
/
antlion.go
58 lines (47 loc) · 1.19 KB
/
antlion.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
package antlion
import (
"html/template"
"strings"
"github.com/endigma/antlion/blocks"
"github.com/endigma/antlion/templates"
)
// Antlion is an instance of the antlion email generator
type Antlion struct {
SubjectPrefix string
}
// Email represents a full renderable email
type Email struct {
Blocks []blocks.Block
PreHeader string
Title string
LogoUrl string
}
func NewEmail(title, preheader string, logourl string, blocks ...blocks.Block) Email {
return Email{
Title: title,
PreHeader: preheader,
Blocks: blocks,
LogoUrl: logourl,
}
}
// RenderText renders an email to plaintext representation
func (e Email) RenderText() string {
var writer strings.Builder
for _, block := range e.Blocks {
writer.WriteString(block.RenderText() + "\n")
}
return writer.String()
}
// RenderHTML renders an email to HTML representation
func (e Email) RenderHTML() string {
var writer strings.Builder
for _, block := range e.Blocks {
writer.WriteString(block.RenderHTML() + "\n")
}
return templates.RenderTemplate("email_layout", map[string]any{
"body": template.HTML(writer.String()),
"preheader": e.PreHeader,
"title": e.Title,
"logo": e.LogoUrl,
})
}