/
example_test.go
42 lines (32 loc) · 1.13 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
package enmime_test
import (
"fmt"
//"net/mail"
"os"
"github.com/cention-sany/go.enmime"
"github.com/cention-sany/net/mail"
)
func Example() {
file, _ := os.Open("test-data/mail/qp-utf8-header.raw")
msg, _ := mail.ReadMessage(file) // Read email using Go's net/mail
mime, _ := enmime.ParseMIMEBody(msg) // Parse message body with enmime
// Headers are in the net/mail Message
fmt.Printf("From: %v\n", msg.Header.Get("From"))
// enmime can decode quoted-printable headers
fmt.Printf("Subject: %v\n", mime.GetHeader("Subject"))
// The plain text body is available as mime.Text
fmt.Printf("Text Body: %v chars\n", len(mime.Text))
// The HTML body is stored in mime.HTML
fmt.Printf("HTML Body: %v chars\n", len(mime.HTML))
// mime.Inlines is a slice of inlined attacments
fmt.Printf("Inlines: %v\n", len(mime.Inlines))
// mime.Attachments contains the non-inline attachments
fmt.Printf("Attachments: %v\n", len(mime.Attachments))
// Output:
// From: James Hillyerd <jamehi03@jamehi03lx.noa.com>
// Subject: MIME UTF8 Test ¢ More Text
// Text Body: 1300 chars
// HTML Body: 1736 chars
// Inlines: 0
// Attachments: 0
}