Permalink
Browse files

[enhance] mime: added ability to recover the html content

  • Loading branch information...
1 parent a6bb931 commit 742a7903f132e814cd6cc9f07e532ac50c5f438e @Aqua-Ye Aqua-Ye committed Feb 29, 2012
Showing with 29 additions and 0 deletions.
  1. +29 −0 stdlib/web/mail/mime.opa
@@ -55,6 +55,7 @@ type Mime.attachment = {
type Mime.body_part =
{ plain : string }
+/ { html : xhtml }
/ { attachment : Mime.attachment }
/ { multipart : list(Mime.entity) }
@@ -294,7 +295,11 @@ Mime = {{
| "multipart/mixed" ->
boundary = Header.extract_value("boundary", content_type_list)
multipart(body, boundary)
+ | "multipart/alternative" ->
+ boundary = Header.extract_value("boundary", content_type_list)
+ multipart(body, boundary)
| "text/plain" -> {plain=decoded_body}
+ | "text/html" -> {html=Xhtml.of_string(decoded_body)}
| _ ->
match Header.find("Content-Disposition", headers)
{none} -> {plain=decoded_body}
@@ -338,6 +343,7 @@ Mime = {{
body_to_string(body:Mime.body_part) : string =
match body
{~plain} -> plain
+ {html=_} -> ""
{attachment=_} -> ""
{multipart=parts} ->
List.map(get_text_aux, parts)
@@ -353,12 +359,35 @@ Mime = {{
get_text(message:Mime.message) =
get_text_aux(message.content)
+ // HTML
+
+ @private
+ body_to_xhtml(body:Mime.body_part) : xhtml =
+ match body
+ {plain=_} -> <></>
+ {~html} -> html
+ {attachment=_} -> <></>
+ {multipart=parts} ->
+ List.map(get_xhtml_aux, parts)
+ |> Xhtml.createFragment(_)
+
+ @private
+ get_xhtml_aux(entity:Mime.entity) =
+ body_to_xhtml(entity.body)
+
+ /**
+ * Get the xhtml content of a MIME message
+ */
+ get_xhtml(message:Mime.message) =
+ get_xhtml_aux(message.content)
+
// Attachments
@private
content_to_attachments(body:Mime.body_part, acc) =
match body
{plain=_} -> acc
+ {html=_} -> acc
{~attachment} -> [attachment|acc]
{multipart=parts} ->
List.fold(get_attachments_aux, parts, acc)

0 comments on commit 742a790

Please sign in to comment.