forked from hectane/hectane
/
attachment.go
48 lines (45 loc) · 1.17 KB
/
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
48
package email
import (
"fmt"
"mime/multipart"
"mime/quotedprintable"
"net/textproto"
)
// Email attachment. The content of the attachment is provided either as a
// UTF-8 string or as a Base64-encoded string ("encoded" set to "true").
type Attachment struct {
Filename string `json:"filename"`
ContentType string `json:"content_type"`
Content string `json:"content"`
Encoded bool `json:"encoded"`
}
// Write the attachment to the specified multipart writer.
func (a Attachment) Write(w *multipart.Writer) error {
headers := make(textproto.MIMEHeader)
if len(a.Filename) != 0 {
headers.Add("Content-Type", fmt.Sprintf("%s; name=%s", a.ContentType, a.Filename))
} else {
headers.Add("Content-Type", a.ContentType)
}
if a.Encoded {
headers.Add("Content-Transfer-Encoding", "base64")
} else {
headers.Add("Content-Transfer-Encoding", "quoted-printable")
}
p, err := w.CreatePart(headers)
if err != nil {
return err
}
if a.Encoded {
if _, err := p.Write([]byte(a.Content)); err != nil {
return err
}
} else {
q := quotedprintable.NewWriter(p)
if _, err := q.Write([]byte(a.Content)); err != nil {
return err
}
return q.Close()
}
return nil
}