-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomail.go
185 lines (140 loc) · 3.52 KB
/
gomail.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
package gomail
import (
"bytes"
"fmt"
"net/smtp"
"sync"
"text/template"
"github.com/Rhaqim/gomail/errors"
)
// Goemail is an interface for sending emails and parsing templates.
type Gomail interface {
// validate validates the email data.
validate(kind ValidateKind) error
// authenticate authenticates the email client.
authenticate() (smtp.Auth, error)
// parseTemplate parses the email template.
parseTemplate() error
// message constructs the email message.
message() []byte
// SendEmail sends an email to the specified recipients with the given subject, template, and data.
SendEmail(mail *Email) error
}
// NewGomail creates a new Gomail instance with the given email authentication configuration and template directory.
func NewGomail(auth EmailAuthConfig, templateDir string) Gomail {
return &GoemailConfig{
Config: auth,
TemplateDir: templateDir,
}
}
func (g *GoemailConfig) validate(kind ValidateKind) error {
switch kind {
case auth:
if g.Config.Host == "" {
return errors.ErrEmptyHost
}
if g.Config.Port == 0 {
return errors.ErrEmptyPort
}
if g.Config.Username == "" {
return errors.ErrEmptyUsername
}
if g.Config.Password == "" {
return errors.ErrEmptyPassword
}
if g.Config.From == "" {
return errors.ErrEmptyFrom
}
if g.TemplateDir == "" {
return errors.ErrEmptyTemplateDir
}
case email:
if len(g.email.Recipients) == 0 {
return errors.ErrEmptyTo
}
}
return nil
}
func (g *GoemailConfig) authenticate() (smtp.Auth, error) {
err := g.validate(auth)
if err != nil {
return nil, err
}
auth := smtp.PlainAuth("", g.Config.Username, g.Config.Password, g.Config.Host)
return auth, nil
}
func (g *GoemailConfig) parseTemplate() error {
var data any
var err error
buf := new(bytes.Buffer)
templFileName := g.TemplateDir + "/" + g.email.TemplateFileName
t, err := template.ParseFiles(templFileName)
if err != nil {
return err
}
if g.email.Data != nil {
data = g.email.Data
} else {
data = struct {
Title string
Body string
}{
Title: g.email.Subject,
Body: g.email.Body,
}
}
if err = t.Execute(buf, data); err != nil {
return err
}
g.email.Body = buf.String()
return nil
}
func (g *GoemailConfig) message() []byte {
var message []byte
subject := "Subject: " + g.email.Subject + "!\n"
// Set the "Content-Type" header to "text/html".
header := make(map[string]string)
header["Content-Type"] = "text/html; charset=\"UTF-8\";"
header["MIME-version"] = "1.0;"
message = []byte("From: " + g.Config.From + "\r\n")
// Add the recipients to the message.
var wg sync.WaitGroup
wg.Add(len(g.email.Recipients))
for _, recipient := range g.email.Recipients {
go func(recipient string) {
defer wg.Done()
g.mutex.Lock()
defer g.mutex.Unlock()
message = append(message, []byte("To: "+recipient+"\r\n")...)
}(recipient)
}
wg.Wait()
message = append(message, []byte(subject+"\r\n")...)
message = append(message, []byte(g.email.Body)...)
// Add the headers to the message.
for k, v := range header {
message = append([]byte(k+": "+v+"\r\n"), message...)
}
return message
}
func (g *GoemailConfig) SendEmail(mail *Email) error {
var err error
g.email = mail
err = g.validate(email)
if err != nil {
return err
}
err = g.parseTemplate()
if err != nil {
return err
}
auth, err := g.authenticate()
if err != nil {
return err
}
err = smtp.SendMail(g.Config.Host+":"+fmt.Sprint(g.Config.Port), auth, g.Config.Username, g.email.Recipients, g.message())
if err != nil {
return err
}
return err
}