cmd/utils.go
func EnvelopeToMarkdown(w io.Writer, e *enmime.Envelope, name string) error {
	md := &markdown{bufio.NewWriter(w)}

	md.H1(name)

	// Output a sorted list of headers, minus the ones displayed later
	md.H2("Header")
	if e.Root != nil && e.Root.Header != nil {
		keys := make([]string, 0, len(e.Root.Header))
		for k := range e.Root.Header {
			switch strings.ToLower(k) {
			case "from", "to", "cc", "bcc", "reply-to", "subject":
				continue
			}
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, k := range keys {
			md.Printf("    %v: %v\n", k, e.GetHeader(k))
		}
	}
	md.Println()

	md.H2("Envelope")
	for _, hkey := range addressHeaders {
		addrlist, err := e.AddressList(hkey)
		if err != nil {
			if err == mail.ErrHeaderNotPresent {
				continue
			}
			return err
		}
		md.H3(hkey)
		for _, addr := range addrlist {
			md.Printf("- %v `<%v>`\n", addr.Name, addr.Address)
		}
		md.Println()
	}
	md.H3("Subject")
	md.Println(e.GetHeader("Subject"))
	md.Println()

	md.H2("Body Text")
	md.Println(e.Text)
	md.Println()

	md.H2("Body HTML")
	md.Println(e.HTML)
	md.Println()

	md.H2("Attachment List")
	for _, a := range e.Attachments {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("Inline List")
	for _, a := range e.Inlines {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("Other Part List")
	for _, a := range e.OtherParts {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("MIME Part Tree")
	if e.Root == nil {
		md.Println("Message was not MIME encoded")
	} else {
		FormatPart(md, e.Root, "    ")
	}

	if len(e.Errors) > 0 {
		md.Println()
		md.H2("Errors")
		for _, perr := range e.Errors {
			md.Println("-", perr)
		}
	}

	return md.Flush()
}
func EnvelopeToMarkdown(w io.Writer, e *enmime.Envelope, name string) error {
	md := &markdown{bufio.NewWriter(w)}

	md.H1(name)

	// Output a sorted list of headers, minus the ones displayed later
	md.H2("Header")
	if e.Root != nil && e.Root.Header != nil {
		keys := make([]string, 0, len(e.Root.Header))
		for k := range e.Root.Header {
			switch strings.ToLower(k) {
			case "from", "to", "cc", "bcc", "reply-to", "subject":
				continue
			}
			keys = append(keys, k)
		}
		sort.Strings(keys)
		for _, k := range keys {
			md.Printf("    %v: %v\n", k, e.GetHeader(k))
		}
	}
	md.Println()

	md.H2("Envelope")
	for _, hkey := range addressHeaders {
		addrlist, err := e.AddressList(hkey)
		if err != nil {
			if err == mail.ErrHeaderNotPresent {
				continue
			}
			return err
		}
		md.H3(hkey)
		for _, addr := range addrlist {
			md.Printf("- %v `<%v>`\n", addr.Name, addr.Address)
		}
		md.Println()
	}
	md.H3("Subject")
	md.Println(e.GetHeader("Subject"))
	md.Println()

	md.H2("Body Text")
	md.Println(e.Text)
	md.Println()

	md.H2("Body HTML")
	md.Println(e.HTML)
	md.Println()

	md.H2("Attachment List")
	for _, a := range e.Attachments {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("Inline List")
	for _, a := range e.Inlines {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("Other Part List")
	for _, a := range e.OtherParts {
		md.Printf("- %v (%v)\n", a.FileName, a.ContentType)
		if a.ContentID != "" {
			md.Printf("  Content-ID: %s\n", a.ContentID)
		}
	}
	md.Println()

	md.H2("MIME Part Tree")
	if e.Root == nil {
		md.Println("Message was not MIME encoded")
	} else {
		FormatPart(md, e.Root, "    ")
	}

	if len(e.Errors) > 0 {
		md.Println()
		md.H2("Errors")
		for _, perr := range e.Errors {
			md.Println("-", perr)
		}
	}

	return md.Flush()
}
// EnvelopeToMarkdown renders the contents of an enmime.Envelope in Markdown format. Used by
// mime-dump and mime-extractor commands.