-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment.go
47 lines (39 loc) · 1009 Bytes
/
attachment.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
package email
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/textproto"
)
type attachment struct {
filename string
contentType string
header textproto.MIMEHeader
content *bytes.Buffer
}
func (at *attachment) setDefaultHeader() {
if at.header == nil {
at.header = make(textproto.MIMEHeader)
}
// set content-type.
ct := "application/octet-stream"
if at.contentType != "" {
ct = at.contentType
}
at.header.Set("Content-Type", ct)
// set disposition
at.header.Set("Content-Disposition", fmt.Sprintf("attachment; filename='%s'", at.filename))
// set content-id
id, _ := generateContentID(at.filename)
at.header.Set("Content-ID", id)
// set Content-Transfer-Encoding as base64.
at.header.Set("Content-Transfer-Encoding", "base64")
}
func (at *attachment) writeBase64To(w io.Writer) {
src := at.content.Bytes()
dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(dst, src)
dst = append(dst, "\r\n"...)
_, _ = w.Write(dst)
}