/
mailer.go
168 lines (153 loc) · 3.58 KB
/
mailer.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
package main
import (
"bytes"
"fmt"
"html/template"
"sync"
"time"
"github.com/vanng822/go-premailer/premailer"
mail "github.com/xhit/go-simple-mail/v2"
)
type Mail struct {
Domain string
Host string
Port int
Username string
Password string
Encryption string
FromAddress string
FromName string
Wait *sync.WaitGroup
MailerChan chan Message
ErrorChan chan error
DoneChan chan bool
}
type Message struct {
From string
FromName string
To string
Subject string
Attachments []string
Data any
DataMap map[string]any
Template string
}
func (app *Config) listenForMail() {
for {
select {
case msg := <-app.Mailer.MailerChan:
go app.Mailer.sendMail(msg, app.Mailer.ErrorChan)
case err := <-app.Mailer.ErrorChan:
app.ErrorLog.Println(err)
case <-app.Mailer.DoneChan:
return
}
}
}
func (m *Mail) sendMail(msg Message, errorChan chan error) {
defer m.Wait.Done()
if msg.Template == "" {
msg.Template = "mail"
}
if msg.From == "" {
msg.From = m.FromAddress
}
if msg.FromName == "" {
msg.FromName = m.FromName
}
msg.DataMap = map[string]any{
"message": msg.Data,
}
formattedMessage, err := m.buildHTMLMessage(msg)
if err != nil {
errorChan <- err
}
plainMessage, err := m.buildPlainTextMessage(msg)
if err != nil {
errorChan <- err
}
server := mail.NewSMTPClient()
server.Host = m.Host
server.Port = m.Port
server.Username = m.Username
server.Password = m.Password
server.Encryption = m.getEncryption(m.Encryption)
server.KeepAlive = false
server.ConnectTimeout = 10 * time.Second
server.SendTimeout = 10 * time.Second
smtpClient, err := server.Connect()
if err != nil {
errorChan <- err
}
email := mail.NewMSG()
email.SetFrom(msg.From).AddTo(msg.To).SetSubject(msg.Subject)
email.SetBody(mail.TextPlain, plainMessage)
email.AddAlternative(mail.TextHTML, formattedMessage)
if len(msg.Attachments) > 0 {
for _, x := range msg.Attachments {
email.AddAttachment(x)
}
}
err = email.Send(smtpClient)
if err != nil {
errorChan <- err
}
}
func (m *Mail) buildHTMLMessage(msg Message) (string, error) {
templateToRender := fmt.Sprintf("./cmd/web/templates/%s.html.gohtml", msg.Template)
t, err := template.New("email-html").ParseFiles(templateToRender)
if err != nil {
return "", err
}
var tpl bytes.Buffer
if err = t.ExecuteTemplate(&tpl, "body", msg.DataMap); err != nil {
return "", nil
}
formattedMessage := tpl.String()
formattedMessage, err = m.inlineCSS(formattedMessage)
if err != nil {
return "", err
}
return formattedMessage, nil
}
func (m *Mail) buildPlainTextMessage(msg Message) (string, error) {
templateToRender := fmt.Sprintf("./cmd/web/templates/%s.plain.gohtml", msg.Template)
t, err := template.New("email-plain").ParseFiles(templateToRender)
if err != nil {
return "", err
}
var tpl bytes.Buffer
if err = t.ExecuteTemplate(&tpl, "body", msg.DataMap); err != nil {
return "", nil
}
plainMessage := tpl.String()
return plainMessage, nil
}
func (m *Mail) inlineCSS(s string) (string, error) {
options := premailer.Options{
RemoveClasses: false,
CssToAttributes: false,
KeepBangImportant: true,
}
prem, err := premailer.NewPremailerFromString(s, &options)
if err != nil {
return "", err
}
html, err := prem.Transform()
if err != nil {
return "", err
}
return html, nil
}
func (m *Mail) getEncryption(e string) mail.Encryption {
switch e {
case "tls":
return mail.EncryptionSTARTTLS
case "ssl":
return mail.EncryptionSSLTLS
case "none":
return mail.EncryptionNone
default:
return mail.EncryptionSTARTTLS
}
}