-
Notifications
You must be signed in to change notification settings - Fork 1
/
mails.go
executable file
·188 lines (139 loc) · 3.32 KB
/
mails.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
186
187
188
package mails
import (
"bytes"
"condo-control/models"
"errors"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/astaxie/beego"
gomail "gopkg.in/gomail.v2"
)
// HTMLParams ...
type HTMLParams struct {
Token string
WatcherName string
SupervisorName string
Date string
URL string
Base64 string
EmptyAssistancesWorkers *models.EmptyAssistancesWorkers
}
//Email ...
type Email struct {
To []string
Bcc []string
Cc []string
Subject string
html string
Headers [][]string
Attachments []string
HTMLParams *HTMLParams
}
var (
from = beego.AppConfig.String("email::from")
name = beego.AppConfig.String("email::name")
pass = beego.AppConfig.String("email::pass")
host = beego.AppConfig.String("email::host")
port, _ = beego.AppConfig.Int("email::port")
)
var (
rootDir, _ = filepath.Abs(beego.AppConfig.String("assets::jumps"))
mailFolderPath = "controllers/services/mails/templates"
mailFolderDir = rootDir + "/" + mailFolderPath
)
func init() {
checkOrCreateImagesFolder(mailFolderDir)
}
/* func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return true, err
} */
func getTemplate(code string, HTMLParams *HTMLParams) (tplHTML *string, err error) {
filesInfo, err := ioutil.ReadDir(mailFolderDir)
if err != nil {
return
}
var tplName string
for _, filesInfo := range filesInfo {
fileInfoName := filesInfo.Name()
fileInfoNameSplited := strings.Split(fileInfoName, ".")
if len(fileInfoNameSplited) == 3 && fileInfoNameSplited[1] == code {
tplName = fileInfoName
break
}
}
if tplName == "" {
err = errors.New("Template is missing")
return
}
tpl, err := template.ParseFiles(mailFolderDir + "/" + tplName)
if err != nil {
return
}
var tplBuffer bytes.Buffer
err = tpl.Execute(&tplBuffer, HTMLParams)
if err != nil {
fmt.Println(err)
return
}
tplString := tplBuffer.String()
return &tplString, nil
}
func (e *Email) loadTemplate(code string) (err error) {
tplHTML, err := getTemplate(code, e.HTMLParams)
if err != nil {
return
}
e.html = *tplHTML
return
}
//SendMails ...
func SendMails(Emails []*Email, code string) {
ch := make(chan *gomail.Message)
for _, Email := range Emails {
go SendMail(Email, code)
}
close(ch)
}
//SendMail ...
func SendMail(Email *Email, code string) (err error) {
m := gomail.NewMessage()
err = Email.loadTemplate(code)
if err != nil {
return
}
for _, header := range Email.Headers {
m.SetHeader(header[0], header[1])
}
for _, Attachment := range Email.Attachments {
m.Attach(Attachment)
}
m.SetHeaders(map[string][]string{
"From": {m.FormatAddress(from, name)},
"To": Email.To,
"Cc": Email.Cc,
"Bcc": Email.Bcc,
"Subject": {Email.Subject},
"X-Date": {m.FormatDate(time.Now())},
})
m.SetBody("text/html", Email.html)
d := gomail.NewDialer(host, port, from, pass)
err = d.DialAndSend(m)
if err != nil {
return
}
return
}
func checkOrCreateImagesFolder(imageFolderDir string) (err error) {
if _, err := os.Stat(imageFolderDir); os.IsNotExist(err) {
os.MkdirAll(imageFolderDir, 644)
}
return
}