-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmime.go
More file actions
386 lines (343 loc) · 11.8 KB
/
Copy pathmime.go
File metadata and controls
386 lines (343 loc) · 11.8 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package email
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"mime"
"mime/quotedprintable"
"net/mail"
"sort"
"strings"
"time"
)
// lookupHeaderFold returns the value of the first header whose key matches
// name case-insensitively. It exists so the Message-ID override works
// regardless of how the caller cased the key.
func lookupHeaderFold(headers map[string]string, name string) (string, bool) {
if v, ok := headers[name]; ok {
return v, true
}
for k, v := range headers {
if strings.EqualFold(k, name) {
return v, true
}
}
return "", false
}
// reservedHeaders lists headers that buildRawMessage writes itself.
// Custom headers with these names are skipped to avoid duplicates.
var reservedHeaders = map[string]struct{}{
"from": {},
"to": {},
"cc": {},
"bcc": {},
"reply-to": {},
"subject": {},
"date": {},
"message-id": {},
"mime-version": {},
"content-type": {},
"content-transfer-encoding": {},
}
const (
// boundaryPrefix is the prefix for MIME boundaries
boundaryPrefix = "boundary-"
// altBoundaryPrefix is the prefix for alternative content boundaries
altBoundaryPrefix = "alt-boundary-"
)
// BuildRawMessage builds a raw RFC 2822 MIME message from an Email.
// Useful for providers that accept raw messages (e.g., AWS SES).
// For DKIM-signed output, use BuildRawMessageWithDKIM.
func BuildRawMessage(e *Email) ([]byte, error) {
if err := e.Validate(); err != nil {
return nil, err
}
return buildRawMessage(e)
}
// BuildRawMessageWithDKIM builds a raw RFC 2822 MIME message and signs it
// with a DKIM-Signature header using the provided config. A nil dkim returns
// the message unsigned (equivalent to BuildRawMessage).
func BuildRawMessageWithDKIM(e *Email, dkim *DKIMConfig) ([]byte, error) {
msg, err := BuildRawMessage(e)
if err != nil {
return nil, err
}
if dkim == nil {
return msg, nil
}
return SignMessage(msg, dkim)
}
// containsCRLF reports whether s contains CR or LF characters.
func containsCRLF(s string) bool {
return strings.ContainsAny(s, "\r\n")
}
// isValidHeaderName reports whether name contains only valid header token characters.
func isValidHeaderName(name string) bool {
if name == "" {
return false
}
for _, r := range name {
// RFC 5322 field-name: visible ASCII except colon
if r < 33 || r > 126 || r == ':' {
return false
}
}
return true
}
// writeCustomHeaders writes user-supplied headers (sorted for deterministic
// output), skipping reserved headers and rejecting names/values that could
// inject additional headers via invalid characters or CR/LF.
func writeCustomHeaders(buf *strings.Builder, headers map[string]string) error {
headerKeys := make([]string, 0, len(headers))
for key := range headers {
headerKeys = append(headerKeys, key)
}
sort.Strings(headerKeys)
for _, key := range headerKeys {
if _, reserved := reservedHeaders[strings.ToLower(key)]; reserved {
continue
}
if !isValidHeaderName(key) {
return fmt.Errorf("invalid header name %q: contains invalid characters", key)
}
value := headers[key]
if containsCRLF(value) {
return fmt.Errorf("invalid header value for %q: contains CR/LF", key)
}
fmt.Fprintf(buf, "%s: %s\r\n", key, value)
}
return nil
}
// buildRawMessage builds the email message with proper MIME encoding.
func buildRawMessage(e *Email) ([]byte, error) {
buf := &strings.Builder{}
// Headers
fmt.Fprintf(buf, "From: %s\r\n", formatAddress(e.From))
fmt.Fprintf(buf, "To: %s\r\n", formatAddressList(e.To))
if len(e.Cc) > 0 {
fmt.Fprintf(buf, "Cc: %s\r\n", formatAddressList(e.Cc))
}
if e.ReplyTo != "" {
fmt.Fprintf(buf, "Reply-To: %s\r\n", formatAddress(e.ReplyTo))
}
fmt.Fprintf(buf, "Subject: %s\r\n", encodeHeader(e.Subject))
// Message-ID: honor a user-supplied value (case-insensitive lookup),
// otherwise generate one using the sender's domain per RFC 5322.
if userMsgID, ok := lookupHeaderFold(e.Headers, "Message-ID"); ok {
fmt.Fprintf(buf, "Message-ID: %s\r\n", userMsgID)
} else {
domain := domainFromAddress(e.From)
msgID := fmt.Sprintf("<%s@%s>", generateUniqueID(), domain)
fmt.Fprintf(buf, "Message-ID: %s\r\n", msgID)
}
// Add Date header
fmt.Fprintf(buf, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))
// Custom headers
if err := writeCustomHeaders(buf, e.Headers); err != nil {
return nil, err
}
// MIME headers
buf.WriteString("MIME-Version: 1.0\r\n")
// Check if we need multipart
hasAttachments := len(e.Attachments) > 0
hasHTML := e.HTMLBody != ""
switch {
case hasAttachments:
// multipart/mixed: body part(s) + attachments
boundary := boundaryPrefix + generateUniqueID()
fmt.Fprintf(buf, "Content-Type: multipart/mixed; boundary=\"%s\"\r\n", boundary)
buf.WriteString("\r\n")
// Body parts
switch {
case e.Body != "" && hasHTML:
// Multipart alternative inside mixed
altBoundary := altBoundaryPrefix + generateUniqueID()
fmt.Fprintf(buf, "--%s\r\n", boundary)
fmt.Fprintf(buf, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n\r\n", altBoundary)
// Plain text
fmt.Fprintf(buf, "--%s\r\n", altBoundary)
buf.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.Body))
buf.WriteString("\r\n\r\n")
// HTML
fmt.Fprintf(buf, "--%s\r\n", altBoundary)
buf.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.HTMLBody))
buf.WriteString("\r\n\r\n")
fmt.Fprintf(buf, "--%s--\r\n", altBoundary)
case hasHTML:
fmt.Fprintf(buf, "--%s\r\n", boundary)
buf.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.HTMLBody))
buf.WriteString("\r\n\r\n")
default:
fmt.Fprintf(buf, "--%s\r\n", boundary)
buf.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.Body))
buf.WriteString("\r\n\r\n")
}
// Attachments
for _, att := range e.Attachments {
contentType := att.ContentType
if contentType == "" {
contentType = "application/octet-stream"
}
if containsCRLF(contentType) {
return nil, fmt.Errorf("invalid attachment content-type for %q: contains CR/LF", att.Filename)
}
fmt.Fprintf(buf, "--%s\r\n", boundary)
fmt.Fprintf(buf, "Content-Type: %s\r\n", contentType)
buf.WriteString("Content-Transfer-Encoding: base64\r\n")
fmt.Fprintf(buf, "Content-Disposition: %s\r\n\r\n", formatDisposition(att.Filename))
// Encode and wrap base64 at 76 characters per RFC 2045
encoded := base64.StdEncoding.EncodeToString(att.Data)
buf.WriteString(wrapText(encoded, 76))
buf.WriteString("\r\n\r\n")
}
fmt.Fprintf(buf, "--%s--\r\n", boundary)
case e.Body != "" && hasHTML:
// multipart/alternative: plain text + HTML (no attachments)
altBoundary := altBoundaryPrefix + generateUniqueID()
fmt.Fprintf(buf, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n", altBoundary)
buf.WriteString("\r\n")
// Plain text
fmt.Fprintf(buf, "--%s\r\n", altBoundary)
buf.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.Body))
buf.WriteString("\r\n\r\n")
// HTML
fmt.Fprintf(buf, "--%s\r\n", altBoundary)
buf.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.HTMLBody))
buf.WriteString("\r\n\r\n")
fmt.Fprintf(buf, "--%s--\r\n", altBoundary)
case hasHTML:
buf.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.HTMLBody))
default:
buf.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
buf.WriteString(quotedPrintableEncode(e.Body))
}
return []byte(buf.String()), nil
}
// wrapText wraps text at the specified width
func wrapText(text string, width int) string {
var result strings.Builder
for i := 0; i < len(text); i += width {
end := i + width
if end > len(text) {
end = len(text)
}
result.WriteString(text[i:end])
if end < len(text) {
result.WriteString("\r\n")
}
}
return result.String()
}
// quotedPrintableEncode encodes text using quoted-printable encoding for safe email transport.
func quotedPrintableEncode(s string) string {
var buf strings.Builder
w := quotedprintable.NewWriter(&buf)
_, _ = w.Write([]byte(s))
_ = w.Close()
return buf.String()
}
// formatAddress returns an RFC 5322 address with any non-ASCII display name
// encoded per RFC 2047. Bare addresses are returned as-is (without angle brackets)
// to preserve the most compact valid form.
func formatAddress(addr string) string {
parsed, err := mail.ParseAddress(addr)
if err != nil {
// Strip CR/LF from malformed address before returning
sanitized := strings.NewReplacer("\r", "", "\n", "").Replace(addr)
return sanitized
}
if parsed.Name == "" {
return parsed.Address
}
return parsed.String()
}
// formatAddressList formats a list of addresses with RFC 2047 encoding applied
// to any non-ASCII display names.
func formatAddressList(addrs []string) string {
out := make([]string, len(addrs))
for i, a := range addrs {
out[i] = formatAddress(a)
}
return strings.Join(out, ", ")
}
// encodeHeader encodes a header value using RFC 2047 if it contains non-ASCII characters.
func encodeHeader(value string) string {
for _, r := range value {
if r > 127 {
return mime.QEncoding.Encode("UTF-8", value)
}
}
return value
}
// filenameReplacer removes characters that could cause header injection in
// Content-Disposition filenames.
var filenameReplacer = strings.NewReplacer(
"\"", "_",
"\r", "",
"\n", "",
"\x00", "",
"/", "_",
"\\", "_",
)
// sanitizeFilename removes characters that could cause header injection in
// Content-Disposition filenames.
func sanitizeFilename(name string) string {
return filenameReplacer.Replace(name)
}
// isASCII reports whether s contains only 7-bit ASCII characters.
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > 127 {
return false
}
}
return true
}
// formatDisposition builds the Content-Disposition header value for an
// attachment. Non-ASCII filenames are encoded per RFC 2231 (filename*=) via
// mime.FormatMediaType so strict clients can decode them; ASCII filenames
// keep the simpler filename="..." form for broader compatibility.
func formatDisposition(name string) string {
clean := sanitizeFilename(name)
if isASCII(clean) {
return fmt.Sprintf(`attachment; filename="%s"`, clean)
}
return mime.FormatMediaType("attachment", map[string]string{"filename": clean})
}
// domainFromAddress extracts the domain part from an email address.
// It handles both bare addresses ("user@example.com") and display-name
// forms ("User <user@example.com>"). Falls back to "localhost" if
// the address cannot be parsed.
func domainFromAddress(addr string) string {
bare := extractAddress(addr)
if idx := strings.LastIndex(bare, "@"); idx >= 0 {
return bare[idx+1:]
}
return "localhost"
}
// generateUniqueID generates a unique identifier for Message-ID and boundaries
func generateUniqueID() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
// Fallback: should never happen with crypto/rand
return fmt.Sprintf("%d", time.Now().UnixNano())
}
return hex.EncodeToString(b)
}