Esempio n. 1
0
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 <*****@*****.**>
	// Subject: MIME UTF8 Test ¢ More Text
	// Text Body: 1300 chars
	// HTML Body: 1736 chars
	// Inlines: 0
	// Attachments: 0
}
Esempio n. 2
0
func dump(reader io.Reader, name string) error {
	// Read email using Go's net/mail
	msg, err := mail.ReadMessage(reader)
	if err != nil {
		return fmt.Errorf("During mail.ReadMessage: %v", err)
	}

	// Parse message body with enmime
	mime, err := enmime.ParseMIMEBody(msg)
	if err != nil {
		return fmt.Errorf("During enmime.ParseMIMEBody: %v", err)
	}

	h1(name)

	h2("Envelope")
	fmt.Printf("From: %v  \n", mime.GetHeader("From"))
	fmt.Printf("To: %v  \n", mime.GetHeader("To"))
	fmt.Printf("Subject: %v  \n", mime.GetHeader("Subject"))
	fmt.Println()

	h2("Body Text")
	fmt.Println(mime.Text)
	fmt.Println()

	h2("Body HTML")
	fmt.Println(mime.HTML)
	fmt.Println()

	h2("Attachment List")
	for _, a := range mime.Attachments {
		newFileName := path.Join(*outdir, a.FileName())
		f, err := os.Create(newFileName)
		if err != nil {
			fmt.Println(err)
		}
		f.Write(a.Content())
		f.Close()
		fmt.Printf("- %v (%v)\n", a.FileName(), a.ContentType())
	}
	fmt.Println()

	h2("MIME Part Tree")
	if mime.Root == nil {
		fmt.Println("Message was not MIME encoded")
	} else {
		printPart(mime.Root, "    ")
	}

	return nil
}
Esempio n. 3
0
// readMessage is a test utility function to fetch a mail.Message object.
func readMessage(filename string) *mail.Message {
	// Open test email for parsing
	raw, err := os.Open(filepath.Join("test-data", "mail", filename))
	if err != nil {
		panic(fmt.Sprintf("Failed to open test data: %v", err))
	}

	// Parse email into a mail.Message object like we do
	reader := bufio.NewReader(raw)
	msg, err := mail.ReadMessage(reader)
	if err != nil {
		panic(fmt.Sprintf("Failed to read message: %v", err))
	}

	return msg
}
Esempio n. 4
0
func dump(reader io.Reader, name string) error {
	// Read email using Go's net/mail
	msg, err := mail.ReadMessage(reader)
	if err != nil {
		return fmt.Errorf("During mail.ReadMessage: %v", err)
	}

	// Parse message body with enmime
	mime, err := enmime.ParseMIMEBody(msg)
	if err != nil {
		return fmt.Errorf("During enmime.ParseMIMEBody: %v", err)
	}

	h1(name)
	h2("Header")
	for k := range msg.Header {
		switch strings.ToLower(k) {
		case "from", "to", "bcc", "subject":
			continue
		}
		fmt.Printf("%v: %v  \n", k, mime.GetHeader(k))
	}

	h2("Envelope")
	for _, hkey := range enmime.AddressHeaders {
		addrlist, err := mime.AddressList(hkey)
		if err != nil {
			if err == mail.ErrHeaderNotPresent {
				continue
			}
			panic(err)
		}
		fmt.Println("### " + hkey)
		for _, addr := range addrlist {
			fmt.Printf("%v <%v>\n", addr.Name, addr.Address)
		}
	}

	fmt.Printf("### Subject\n %v\n", mime.GetHeader("Subject"))

	h2("Body Text")
	fmt.Println(mime.Text)
	fmt.Println()

	h2("Body HTML")
	fmt.Println(mime.HTML)
	fmt.Println()

	h2("Attachment List")
	for _, a := range mime.Attachments {
		fmt.Printf("- %v (%v)\n", a.FileName(), a.ContentType())
	}
	fmt.Println()

	h2("MIME Part Tree")
	if mime.Root == nil {
		fmt.Println("Message was not MIME encoded")
	} else {
		printPart(mime.Root, "    ")
	}

	return nil
}