-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap.go
328 lines (284 loc) · 6.45 KB
/
imap.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package inbox
import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"net/smtp"
"strings"
"github.com/emersion/go-imap/v2"
client "github.com/emersion/go-imap/v2/imapclient"
)
type Config struct {
SmtpPort int
SmtpServer string
ImapPort int
ImapServer string
}
type Email struct {
Config
sc *smtp.Client
rc *client.Client
}
type Subject struct {
From []string // 发送者
To []string // 接收者
Title string // 标题
Content string // 内容
}
func GmailConfig() Config {
return Config{
SmtpPort: 587,
SmtpServer: "smtp.gmail.com",
ImapPort: 993,
ImapServer: "imap.gmail.com",
}
}
func OutlookConfig() Config {
return Config{
SmtpPort: 587,
SmtpServer: "smtp.office365.com",
ImapPort: 993,
ImapServer: "outlook.office365.com",
}
}
func QMailConfig() Config {
return Config{
SmtpPort: 587,
SmtpServer: "smtp.qq.com",
ImapPort: 993,
ImapServer: "imap.qq.com",
}
}
func New(c Config) Email {
return Email{c, nil, nil}
}
func (c Config) smtpAdder() string {
return fmt.Sprintf("%s:%d", c.SmtpServer, c.SmtpPort)
}
func (c Config) imapAdder() string {
return fmt.Sprintf("%s:%d", c.ImapServer, c.ImapPort)
}
// 登陆邮箱
func (e *Email) Login(user, passwd string) error {
// 发送端
{
// 创建一个 SMTP 客户端
send, err := smtp.Dial(e.smtpAdder())
if err != nil {
return err
}
// 启用 TLS 加密
if err = send.StartTLS(&tls.Config{
ServerName: e.SmtpServer,
InsecureSkipVerify: true,
}); err != nil {
return err
}
// 登录您的账号和密码
if err = send.Auth(smtp.PlainAuth("", user, passwd, e.SmtpServer)); err != nil {
return err
}
e.sc = send
}
// 接收端
{
// 创建一个 IMAP 客户端
recv, err := client.DialTLS(e.imapAdder(), nil)
if err != nil {
e.Release()
return err
}
// 登录您的账号和密码
cmd := recv.Login(user, passwd)
if err = cmd.Wait(); err != nil {
e.Release()
return err
}
e.rc = recv
}
return nil
}
// 退出登陆
func (e *Email) Release() {
if e.sc != nil {
_ = e.sc.Close()
e.sc = nil
}
if e.rc != nil {
_ = e.rc.Logout()
e.rc = nil
}
}
func (e *Email) RecvMessage(box string, readOnly bool, criteria *imap.SearchCriteria) ([]Subject, error) {
if e.rc == nil {
return nil, errors.New("do it after login")
}
cmd := e.rc.Select(box, &imap.SelectOptions{
ReadOnly: readOnly,
})
selectRows, err := cmd.Wait()
if err != nil {
return nil, err
}
numMessages := int(selectRows.NumMessages)
if numMessages == 0 {
return nil, errors.New("no mail")
}
// 搜索您想要获取的邮件
searchRows, err := e.rc.Search(criteria, nil).Wait()
if err != nil {
return nil, err
}
ids := searchRows.AllSeqNums()
numMessages = len(ids)
if numMessages == 0 {
return nil, errors.New("no mail")
}
// 只读10条
var maxLen = 10
if numMessages < maxLen {
maxLen = numMessages
}
limit := imap.SeqSetNum(ids[numMessages-maxLen:]...)
var messages []*client.FetchMessageBuffer
// 获取邮件的内容
fetchOptions := &imap.FetchOptions{
Flags: true,
Envelope: true,
BodyStructure: &imap.FetchItemBodyStructure{Extended: true},
BodySection: []*imap.FetchItemBodySection{
{
Specifier: imap.PartSpecifierHeader,
},
{
Part: []int{1, 2, 3},
},
},
}
// 获取数据
fetchCmd := e.rc.Fetch(limit, fetchOptions)
if messages, err = fetchCmd.Collect(); err != nil {
return nil, err
}
var subjects []Subject
for _, messageBuffer := range messages {
{
content, er := messageString(messageBuffer)
if er != nil {
return subjects, er
}
subjects = append(subjects, Subject{
From: addrStrings(messageBuffer.Envelope.From),
To: addrStrings(messageBuffer.Envelope.To),
Title: messageBuffer.Envelope.Subject,
Content: content,
})
}
// 标记邮件为已读
store := e.rc.Store
seqSet := imap.SeqSetNum(messageBuffer.SeqNum)
storeItems := imap.StoreFlags{
Op: imap.StoreFlagsAdd,
Flags: []imap.Flag{
imap.FlagSeen,
},
}
storeCmd := store(seqSet, &storeItems, nil)
if er := storeCmd.Wait(); er != nil {
return subjects, er
}
}
return subjects, nil
}
func messageString(messageBuffer *client.FetchMessageBuffer) (string, error) {
if messageBuffer == nil {
return "", nil
}
multiPart, ok := messageBuffer.BodyStructure.(*imap.BodyStructureMultiPart)
if ok {
index := 0
for _, section := range messageBuffer.BodySection {
if index == 1 {
structure, ok := multiPart.Children[0].(*imap.BodyStructureSinglePart)
if ok {
if structure.Encoding == "BASE64" {
decodeBytes, err := base64.StdEncoding.DecodeString(string(section))
if err != nil {
return "", err
}
return string(decodeBytes), nil
}
return string(section), nil
}
}
index++
}
}
singlePart, ok := messageBuffer.BodyStructure.(*imap.BodyStructureSinglePart)
if ok {
index := 0
for _, section := range messageBuffer.BodySection {
if index == 1 {
if singlePart.Encoding == "BASE64" {
decodeBytes, err := base64.StdEncoding.DecodeString(string(section))
if err != nil {
return "", err
}
return string(decodeBytes), nil
}
return string(section), nil
}
index++
}
}
return "", nil
}
func (e *Email) SendMessage(from, to, subject, body string) error {
if e.sc == nil {
return errors.New("please login")
}
return e.sendMessage("text/plain", from, to, subject, body)
}
func (e *Email) SendHtmlMessage(from, to, subject, body string) error {
if e.sc == nil {
return errors.New("please login")
}
return e.sendMessage("text/html", from, to, subject, body)
}
func (e *Email) sendMessage(contentType, from, to, subject, body string) error {
// 发送一封邮件
header := make(map[string]string)
header["From"] = from
header["To"] = to
header["Subject"] = subject
header["Content-Type"] = contentType + "; charset=UTF-8"
var message strings.Builder
for k, v := range header {
message.WriteString(fmt.Sprintf("%s: %s\r\n", k, v))
}
message.WriteString("\r\n")
message.WriteString(body)
if err := e.sc.Mail(from); err != nil {
return err
}
if err := e.sc.Rcpt(to); err != nil {
return err
}
w, err := e.sc.Data()
if err != nil {
return err
}
defer w.Close()
if _, err = w.Write([]byte(message.String())); err != nil {
return err
}
return nil
}
func addrStrings(addr []imap.Address) []string {
var buf []string
for _, value := range addr {
buf = append(buf, value.Addr())
}
return buf
}