forked from emersion/go-message
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
138 lines (116 loc) · 2.78 KB
/
example_test.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
package message_test
import (
"bytes"
"io"
"log"
"strings"
"github.com/emersion/go-message"
)
func ExampleRead() {
// Let's assume r is an io.Reader that contains a message.
var r io.Reader
m, err := message.Read(r)
if message.IsUnknownCharset(err) {
// This error is not fatal
log.Println("Unknown encoding:", err)
} else if err != nil {
log.Fatal(err)
}
if mr := m.MultipartReader(); mr != nil {
// This is a multipart message
log.Println("This is a multipart message containing:")
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
t, _, _ := p.Header.ContentType()
log.Println("A part with type", t)
}
} else {
t, _, _ := m.Header.ContentType()
log.Println("This is a non-multipart message with type", t)
}
}
func ExampleWriter() {
var b bytes.Buffer
var h message.Header
h.SetContentType("multipart/alternative", nil)
w, err := message.CreateWriter(&b, h)
if err != nil {
log.Fatal(err)
}
var h1 message.Header
h1.SetContentType("text/html", nil)
w1, err := w.CreatePart(h1)
if err != nil {
log.Fatal(err)
}
io.WriteString(w1, "<h1>Hello World!</h1><p>This is an HTML part.</p>")
w1.Close()
var h2 message.Header
h1.SetContentType("text/plain", nil)
w2, err := w.CreatePart(h2)
if err != nil {
log.Fatal(err)
}
io.WriteString(w2, "Hello World!\n\nThis is a text part.")
w2.Close()
w.Close()
log.Println(b.String())
}
func Example_transform() {
// Let's assume r is an io.Reader that contains a message.
var r io.Reader
m, err := message.Read(r)
if message.IsUnknownCharset(err) {
log.Println("Unknown encoding:", err)
} else if err != nil {
log.Fatal(err)
}
// We'll add "This message is powered by Go" at the end of each text entity.
poweredBy := "\n\nThis message is powered by Go."
var b bytes.Buffer
w, err := message.CreateWriter(&b, m.Header)
if err != nil {
log.Fatal(err)
}
// Define a function that transforms message.
var transform func(w *message.Writer, e *message.Entity) error
transform = func(w *message.Writer, e *message.Entity) error {
if mr := e.MultipartReader(); mr != nil {
// This is a multipart entity, transform each of its parts
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
return err
}
pw, err := w.CreatePart(p.Header)
if err != nil {
return err
}
if err := transform(pw, p); err != nil {
return err
}
pw.Close()
}
return nil
} else {
body := e.Body
if strings.HasPrefix(m.Header.Get("Content-Type"), "text/") {
body = io.MultiReader(body, strings.NewReader(poweredBy))
}
_, err := io.Copy(w, body)
return err
}
}
if err := transform(w, m); err != nil {
log.Fatal(err)
}
w.Close()
log.Println(b.String())
}