-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimeparts.go
90 lines (73 loc) · 2.18 KB
/
mimeparts.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
package bart
const boundary = "=_=_=dKJUIzdDfdjILoJdsOqQYYXSs4RXbH0WgUL"
const delimiter = "\r\n--" + boundary + "\r\n"
const encodingBase64 = "Content-Transfer-Encoding: base64\r\n"
const closingDelimiter = "\r\n--" + boundary + "--\r\n"
type emailHeader struct {
content string
}
func (h *emailHeader) asPlainBytes() []byte {
return []byte(h.content)
}
func (h *emailHeader) asHtml() []byte {
res := []byte("<!DOCTYPE html>\r\n" +
"<html><head><meta charset=\"UTF-8\"></head>\r\n" +
"<pre style=\"background-color: #f0f8ff;\">\r\n")
res = append(res, EscapeHtmlCharacters(h.asPlainBytes())...)
return append(res, []byte("</pre>\r\n")...)
}
func footerAsPlainBytes() []byte {
return []byte(closingDelimiter)
}
func footerAsHtml() []byte {
return []byte("<pre style=\"background-color: #f0f8ff;\">" +
closingDelimiter +
"</pre></html>\r\n")
}
type mimepart interface {
asPlainBytes() []byte
asHtml() []byte
asBase64() []byte
contentType() string
}
type textHtml struct {
content string
}
func (p *textHtml) asPlainBytes() []byte {
return []byte(delimiter + p.contentType() + p.content)
}
func (p *textHtml) asBase64() []byte {
res := []byte(delimiter + encodingBase64 + p.contentType())
return append(res, EncodeToBase64WrapLines(p.content)...)
}
func (p *textHtml) asHtml() []byte {
return []byte("<pre style=\"background-color: #fff0f5;\">" +
delimiter +
p.contentType() +
"</pre>\r\n" +
p.content)
}
func (*textHtml) contentType() string {
return "Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n"
}
type textPlain struct {
content string
}
func (p *textPlain) asPlainBytes() []byte {
return []byte(delimiter + p.contentType() + p.content)
}
func (p *textPlain) asBase64() []byte {
res := []byte(delimiter + encodingBase64 + p.contentType())
return append(res, EncodeToBase64WrapLines(p.content)...)
}
func (p *textPlain) asHtml() []byte {
res := []byte("<pre style=\"background-color: #ffefd5;\">" +
delimiter +
p.contentType() +
"</pre>\r\n<pre>\r\n")
res = append(res, EscapeHtmlCharacters([]byte(p.content))...)
return append(res, []byte("</pre>\r\n")...)
}
func (*textPlain) contentType() string {
return "Content-Type: text/plain; charset=\"UTF-8\"\r\n\r\n"
}