/
mail.go
executable file
·107 lines (91 loc) · 2.44 KB
/
mail.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
package mail
import (
"bytes"
"context"
"html/template"
"net/smtp"
"github.com/vilisseranen/castellers/common"
"go.opentelemetry.io/otel"
)
var tracer = otel.Tracer("castellers")
type emailInfo struct {
Header emailHeader
Top emailTop
MainSections []emailMain
Actions []emailAction
Bottom emailBottom
ImageSource string
}
type emailHeader struct {
Title string
}
// To should probably go to emailHeader
type emailTop struct {
Title string
Subtitle string
To string
}
type emailMain struct {
Title string
Subtitle string
Text string
Author string
}
type emailAction struct {
Title string
Text string
Buttons []Button
}
type Button struct {
Text string
Link string
Color string
}
type emailBottom struct {
ProfileLink string
MyProfile string
Suggestions string
}
func unescape(s string) template.HTML {
return template.HTML(s)
}
func (e emailInfo) buildEmail() (string, error) {
t, err := template.New("email.html").Funcs(template.FuncMap{"unescape": unescape}).ParseFiles("mail/templates/email.html")
if err != nil {
common.Error("Error parsing template: " + err.Error())
return "", err
}
// t.Funcs(template.FuncMap{"unescape": unescape})
buffer := new(bytes.Buffer)
if err = t.Execute(buffer, e); err != nil {
common.Error("Error generating template: " + err.Error())
return "", err
}
header := buildHeader(e.Header.Title, e.Top.To)
return header + buffer.String(), nil
}
func buildHeader(title, to string) string {
return "Subject: " + title + "\r\n" +
"To: " + to + "\r\n" +
"From: Castellers de Montreal <" + common.GetConfigString("smtp.username") + ">\r\n" +
"Reply-To: " + common.GetConfigString("reply_to") + "\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Type: text/html; charset=\"UTF-8\";\r\n" +
"\r\n"
}
func sendMail(ctx context.Context, email emailInfo) error {
_, span := tracer.Start(ctx, "mail.sendMail")
defer span.End()
body, err := email.buildEmail()
if err != nil {
common.Error("Cannot build Email")
return err
}
auth := smtp.PlainAuth("", common.GetConfigString("smtp.username"), common.GetConfigString("smtp.password"), common.GetConfigString("smtp.server"))
addr := common.GetConfigString("smtp.server") + ":" + common.GetConfigString("smtp.port")
if err := smtp.SendMail(addr, auth, common.GetConfigString("smtp.username"), []string{email.Top.To}, []byte(body)); err != nil {
common.Error(err.Error())
return err
}
return nil
}