extractText walks the MIME tree and returns the best text representation — the first text/plain part, or (failing that) the first text/html rendered to text — together with the first text/html part decoded but NOT rendered to text (htmlRaw, for display). Falls back to the raw body if MIME parsing fa
(raw []byte)
| 106 | // text — together with the first text/html part decoded but NOT rendered to |
| 107 | // text (htmlRaw, for display). Falls back to the raw body if MIME parsing fails. |
| 108 | func extractText(raw []byte) (text, htmlRaw string) { |
| 109 | msg, err := mail.ReadMessage(bytes.NewReader(raw)) |
| 110 | if err != nil { |
| 111 | return string(raw), "" |
| 112 | } |
| 113 | plain, htmlText, htmlRaw := walkParts(msg.Header.Get("Content-Type"), msg.Header.Get("Content-Transfer-Encoding"), msg.Body, 0) |
| 114 | if plain != "" { |
| 115 | return plain, htmlRaw |
| 116 | } |
| 117 | if htmlText != "" { |
| 118 | return htmlText, htmlRaw |
| 119 | } |
| 120 | // No recognized text part — return the decoded top-level body. |
| 121 | b, _ := io.ReadAll(msg.Body) |
| 122 | return string(b), htmlRaw |
| 123 | } |
| 124 | |
| 125 | // walkParts returns the first text/plain, the first text/html rendered to text, |
| 126 | // and the first text/html decoded as-is (htmlRaw) found anywhere in the |