+ let is_text_plain hdr =
+ try
+ let ct = hdr#field "content-type" in
+ String.starts_with ct "text/plain"
+ with
+ Not_found -> true in
+
+ (* Find the first text/plain body. *)
+ let rec find_body = function
+ (header, `Body mime_body) when is_text_plain header ->
+ mime_body#value
+ | (_, `Body _) -> raise Not_found
+ | (_, `Parts []) -> raise Not_found (* should never happen *)
+ | (_, `Parts (m :: _)) ->
+ find_body m in
+ let content =
+ try
+ let text = find_body msg in
+ let lines = Pcre.split ~rex:lines_re text in
+ let lines = List.map trim lines in
+ let lines = List.map (fun str -> str ^ "<br>") lines in
+ String.concat "\n" lines
+ with
+ Not_found ->
+ "No plain text message body found" in