/
parser.go
37 lines (26 loc) · 1 KB
/
parser.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
package main
import (
"fmt"
"net/mail"
"os"
"github.com/jhillyerd/go.enmime"
)
func main() {
file, _ := os.Open("/Users/rbd/Temp/7ccaa5be-acf7-45c7-a289-c6bd2cb0492a.eml")
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"))
fmt.Printf("From: %v\n", mime.GetHeader("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))
fmt.Printf("Text Body: %s\n", 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))
}