Attachments walks the MIME tree of a raw RFC 5322 message and returns its attachment parts in stable document order (depth-first, as they appear in the wire bytes). Index N from this function is the SAME index used by AttachmentAt and by the message read view — it is the authoritative attachment ind
(raw []byte)
| 32 | // On a parse failure or a non-multipart message with no attachment part, returns |
| 33 | // an empty slice (never nil-panics). |
| 34 | func Attachments(raw []byte) []Attachment { |
| 35 | msg, err := mail.ReadMessage(bytes.NewReader(raw)) |
| 36 | if err != nil { |
| 37 | return nil |
| 38 | } |
| 39 | var out []Attachment |
| 40 | collectAttachments( |
| 41 | msg.Header.Get("Content-Type"), |
| 42 | msg.Header.Get("Content-Transfer-Encoding"), |
| 43 | msg.Header.Get("Content-Disposition"), |
| 44 | "", // top-level has no part filename |
| 45 | msg.Body, |
| 46 | 0, |
| 47 | &out, |
| 48 | ) |
| 49 | return out |
| 50 | } |
| 51 | |
| 52 | // AttachmentAt returns the attachment at the 0-based index, or false if out of |
| 53 | // range. It re-walks the message (the caller holds the raw bytes); attachments |