ExtractForwardContext parses an RFC 5322 raw message and pulls out the fields needed to compose a forward quote. Parse failures degrade gracefully — the returned context's body fields stay empty so the caller still gets a usable header block to prepend.
(rawMessage []byte)
| 34 | // gracefully — the returned context's body fields stay empty so the |
| 35 | // caller still gets a usable header block to prepend. |
| 36 | func ExtractForwardContext(rawMessage []byte) ForwardContext { |
| 37 | ctx := ForwardContext{} |
| 38 | if len(rawMessage) == 0 { |
| 39 | return ctx |
| 40 | } |
| 41 | |
| 42 | msg, err := mail.ReadMessage(bytes.NewReader(rawMessage)) |
| 43 | if err != nil { |
| 44 | return ctx |
| 45 | } |
| 46 | |
| 47 | ctx.From = strings.TrimSpace(msg.Header.Get("From")) |
| 48 | ctx.Date = strings.TrimSpace(msg.Header.Get("Date")) |
| 49 | ctx.Subject = strings.TrimSpace(msg.Header.Get("Subject")) |
| 50 | ctx.To = strings.TrimSpace(msg.Header.Get("To")) |
| 51 | ctx.Cc = strings.TrimSpace(msg.Header.Get("Cc")) |
| 52 | |
| 53 | contentType := msg.Header.Get("Content-Type") |
| 54 | encoding := msg.Header.Get("Content-Transfer-Encoding") |
| 55 | ctx.Text, ctx.HTML = extractBodyParts(msg.Body, contentType, encoding) |
| 56 | return ctx |
| 57 | } |
| 58 | |
| 59 | // extractBodyParts walks a message body looking for the text/plain and |
| 60 | // text/html parts. Recurses into multipart/alternative and |