-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
236 lines (205 loc) · 4.57 KB
/
email.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package email
import (
"fmt"
"net/mail"
"path/filepath"
"strings"
"time"
"github.com/askasoft/pango/fsu"
"github.com/askasoft/pango/iox"
"github.com/askasoft/pango/str"
)
// Email email struct
type Email struct {
MsgID string
Date time.Time
Subject string
Message string
HTML bool
Attachments []*Attachment
sender *mail.Address
from *mail.Address
tos []*mail.Address
ccs []*mail.Address
bccs []*mail.Address
replys []*mail.Address
DkimDomain string
DkimSelector string
DkimPrivateKey string
}
// ParseAddress parse email address
func ParseAddress(s string) (*mail.Address, error) {
a, err := mail.ParseAddress(s)
if err != nil {
return nil, fmt.Errorf("Invalid address %s - %w", s, err)
}
return a, nil
}
// GetDate return the email date
func (m *Email) GetDate() time.Time {
if m.Date.IsZero() {
m.Date = time.Now()
}
return m.Date
}
// GetSender get sender
func (m *Email) GetSender() string {
if m.sender == nil {
m.sender = m.from
}
if m.sender == nil {
return ""
}
return m.sender.Address
}
// SetSender set sender
func (m *Email) SetSender(s string) error {
a, err := ParseAddress(s)
if err != nil {
m.sender = a
}
return err
}
// GetFrom get from
func (m *Email) GetFrom() *mail.Address {
return m.from
}
// SetFrom set from
func (m *Email) SetFrom(s string) error {
a, err := ParseAddress(s)
if err == nil {
m.from = a
}
return err
}
// GetTos get to address array
func (m *Email) GetTos() []*mail.Address {
return m.tos
}
// AddTo add to address
func (m *Email) AddTo(tos ...string) error {
for _, s := range tos {
a, err := ParseAddress(s)
if err != nil {
return err
}
m.tos = append(m.tos, a)
}
return nil
}
// GetCcs get cc address array
func (m *Email) GetCcs() []*mail.Address {
return m.ccs
}
// AddCc add cc address
func (m *Email) AddCc(ccs ...string) error {
for _, s := range ccs {
a, err := ParseAddress(s)
if err != nil {
return err
}
m.ccs = append(m.ccs, a)
}
return nil
}
// GetBccs get bcc address array
func (m *Email) GetBccs() []*mail.Address {
return m.bccs
}
// AddBcc add bcc address
func (m *Email) AddBcc(bccs ...string) error {
for _, s := range bccs {
a, err := ParseAddress(s)
if err != nil {
return err
}
m.bccs = append(m.bccs, a)
}
return nil
}
// GetReplys get reply address array
func (m *Email) GetReplys() []*mail.Address {
return m.replys
}
// AddReply add reply address
func (m *Email) AddReply(rs ...string) error {
for _, s := range rs {
a, err := ParseAddress(s)
if err != nil {
return err
}
m.replys = append(m.replys, a)
}
return nil
}
// AddAttachment add a attachment
func (m *Email) AddAttachment(as ...*Attachment) {
m.Attachments = append(m.Attachments, as...)
}
// AttachFile attach a file
func (m *Email) AttachFile(paths ...string) error {
for _, path := range paths {
if err := fsu.FileExists(path); err != nil {
return err
}
m.AddAttachment(&Attachment{Name: filepath.Base(path), Data: &iox.FileReader{Path: path}})
}
return nil
}
// SetTextMsg set the text message
func (m *Email) SetTextMsg(msg string) {
m.HTML = false
m.Message = msg
}
// SetHTMLMsg set the HTML message
func (m *Email) SetHTMLMsg(msg string) {
m.HTML = true
m.Message = msg
}
// AttachString attach a string date file
func (m *Email) AttachString(name string, data string) {
m.AddAttachment(&Attachment{Name: name, Data: strings.NewReader(data)})
}
// EmbedFile embed a file
func (m *Email) EmbedFile(cid string, path string) error {
if err := fsu.FileExists(path); err != nil {
return err
}
m.AddAttachment(&Attachment{Cid: cid, Name: filepath.Base(path), Data: &iox.FileReader{Path: path}})
return nil
}
// EmbedString embed a string date file
func (m *Email) EmbedString(cid string, name string, data string) {
m.AddAttachment(&Attachment{Cid: cid, Name: name, Data: strings.NewReader(data)})
}
// GetRecipients get all recipients
func (m *Email) GetRecipients() []string {
rs := make(map[string]bool, len(m.tos)+len(m.ccs)+len(m.bccs))
for _, a := range m.tos {
rs[a.Address] = true
}
for _, a := range m.ccs {
rs[a.Address] = true
}
for _, a := range m.bccs {
rs[a.Address] = true
}
as := make([]string, 0, len(rs))
for a := range rs {
as = append(as, a)
}
return as
}
// GetRecipientsByDomain return a (domain => address string array) map
func (m *Email) GetRecipientsByDomain() map[string][]string {
rcpts := m.GetRecipients()
rs := make(map[string][]string, len(rcpts))
for _, r := range rcpts {
d := str.SubstrAfterByte(r, '@')
if d == "" {
continue
}
rs[d] = append(rs[d], r)
}
return rs
}