-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmsg.go
244 lines (207 loc) · 4.78 KB
/
msg.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
package mailx
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"io"
"mime"
"net/mail"
"runtime"
"time"
)
// @author valor.
const (
charset = "utf-8"
headerEncoder = mime.BEncoding
multipartEncoding = "base64"
)
var multipartWriter = func(w io.Writer) io.WriteCloser {
return base64.NewEncoder(base64.StdEncoding, &multipartBase64Writer{w: w})
}
// CopyFunc is the function that runs when the message is sent.
// It should copy the content of the emails to the io.Writer(SMTP).
type CopyFunc func(io.Writer) (int, error)
// Message represents an email message.
type Message struct {
header *header
parts []*part
files []*file
}
func (m *Message) sender() (string, error) {
if m.header == nil || m.header.from == nil {
return "", errors.New("empty email sender")
}
sender := m.header.from.Address
if sender == "" {
return "", errors.New("empty email sender")
}
return sender, nil
}
func (m *Message) rcpt() ([]string, error) {
lenTo := len(m.header.to)
lenCc := len(m.header.cc)
lenBcc := len(m.header.bcc)
total := lenTo + lenCc + lenBcc
if total == 0 {
return nil, errors.New("empty email rcpt")
}
rcpt := make([]string, 0, total)
if lenTo > 0 {
for _, address := range m.header.to {
rcpt = append(rcpt, address.Address)
}
}
if lenCc > 0 {
for _, address := range m.header.cc {
rcpt = append(rcpt, address.Address)
}
}
if lenBcc > 0 {
for _, address := range m.header.bcc {
rcpt = append(rcpt, address.Address)
}
}
return rcpt, nil
}
type part struct {
ctype string // Content-Type
copier CopyFunc
}
func (p *part) contentType() string {
return p.ctype + "; charset=" + charset
}
type header struct {
from *mail.Address
to []*mail.Address
cc []*mail.Address
bcc []*mail.Address
singleRecvAddr bool
subject string
datefmt string
ua string
extra map[string][]string
}
func (h *header) presets() []string {
return []string{"FROM", "TO", "CC", "BCC", "SUBJECT", "DATE", "MIME-VERSION", "USER-AGENT", "MESSAGE-ID"}
}
// date returns a valid RFC 5322 date.
func (h *header) date() string {
if h.datefmt == "" {
return time.Now().Format(time.RFC1123Z)
}
return h.datefmt
}
// mimeVersion returns MIME-VERSION
func (h *header) mimeVersion() string {
return "1.0 (Produced by Mailx)"
}
func (h *header) messageId() (string, error) {
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
return "", err
}
return "<--" + hex.EncodeToString(buf[:]) + "@GolangMailxMessageID>", nil
}
func (h *header) userAgent() string {
if h.ua == "" {
return "github/valord577/mailx " + runtime.Version() + " " + runtime.GOOS + "/" + runtime.GOARCH
}
return h.ua
}
func (h *header) writeTo(w io.Writer) (int, error) {
if len(h.to) == 0 {
return 0, errors.New("empty email header: 'TO'")
}
if h.subject == "" {
return 0, errors.New("empty email header: 'SUBJECT'")
}
mid, err := h.messageId()
if err != nil {
return 0, errors.New("failed to generate 'MESSAGE-ID': " + err.Error())
}
b := &bytes.Buffer{}
// MESSAGE-ID
b.WriteString("MESSAGE-ID: ")
b.WriteString(headerEncoder.Encode(charset, mid))
b.WriteString("\r\n")
// FROM
b.WriteString("FROM: ")
b.WriteString(h.from.String())
b.WriteString("\r\n")
// TO
length := len(h.to)
if length == 1 || !h.singleRecvAddr {
for _, to := range h.to {
b.WriteString("TO: ")
b.WriteString(to.String())
b.WriteString("\r\n")
}
} else {
b.WriteString("TO: ")
for i, to := range h.to {
b.WriteString(to.String())
if i < length-1 {
b.WriteString(",")
}
}
b.WriteString("\r\n")
}
// CC
length = len(h.cc)
if length > 0 {
if length == 1 || !h.singleRecvAddr {
for _, cc := range h.cc {
b.WriteString("CC: ")
b.WriteString(cc.String())
b.WriteString("\r\n")
}
} else {
b.WriteString("CC: ")
for i, cc := range h.cc {
b.WriteString(cc.String())
if i < length-1 {
b.WriteString(",")
}
}
b.WriteString("\r\n")
}
}
// SUBJECT
b.WriteString("SUBJECT: ")
b.WriteString(headerEncoder.Encode(charset, h.subject))
b.WriteString("\r\n")
// DATE
b.WriteString("DATE: ")
b.WriteString(headerEncoder.Encode(charset, h.date()))
b.WriteString("\r\n")
// MIME-VERSION
b.WriteString("MIME-VERSION: ")
b.WriteString(headerEncoder.Encode(charset, h.mimeVersion()))
b.WriteString("\r\n")
// USER-AGENT
b.WriteString("USER-AGENT: ")
b.WriteString(headerEncoder.Encode(charset, h.userAgent()))
b.WriteString("\r\n")
// extra headers
length = len(h.extra)
if length > 0 {
for _, key := range h.presets() {
delete(h.extra, key)
}
}
length = len(h.extra)
if length > 0 {
for k, vs := range h.extra {
for _, v := range vs {
b.WriteString(k)
b.WriteString(": ")
b.WriteString(headerEncoder.Encode(charset, v))
b.WriteString("\r\n")
}
}
}
return w.Write(b.Bytes())
}