-
-
Notifications
You must be signed in to change notification settings - Fork 619
/
message.go
41 lines (34 loc) · 961 Bytes
/
message.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
package email
import (
"bytes"
"context"
"strings"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/tpl"
)
// Message represents what is sent by email
type Message struct {
Subject string
Body string
}
// RenderMessage returns the HTML of an email based on template and params
func RenderMessage(ctx context.Context, templateName string, fromAddress string, params dto.Props) *Message {
noreply := false
if fromAddress == NoReply {
noreply = true
}
tmpl := tpl.GetTemplate("/views/email/base_email.html", "/views/email/"+templateName+".html")
var bf bytes.Buffer
if err := tpl.Render(ctx, tmpl, &bf, params.Merge(dto.Props{
"logo": params["logo"],
"noreply": noreply,
})); err != nil {
panic(err)
}
lines := strings.Split(bf.String(), "\n")
body := strings.TrimLeft(strings.Join(lines[2:], "\n"), " ")
return &Message{
Subject: strings.TrimLeft(lines[0], "subject: "),
Body: body,
}
}