Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Johann Spies <jspies@sun.ac.za>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Netmime: splitting email attachments
Date: 21 Jan 2004 16:21:16 +0100	[thread overview]
Message-ID: <1074698477.3580.58.camel@ares> (raw)
In-Reply-To: <20040120131231.GE22271@sun.ac.za>

Am Die, 2004-01-20 um 14.12 schrieb Johann Spies:
> I have asked a similar question on ocaml_beginners, but did not get
> any reaction.
> 
> I want to split attachments containing messages I want to feed to
> SA-learn into different files.  I have spent hours now to try and
> understand the documentation of netmime and family of classes/modules.
> I would appreciate some help please. Fortunately I saw one or two
> examples in the documentation which helped me so far.
> 
> I have come so far:
> 
> # let readmessage f = Netmime.read_mime_message (new Netstream.input_stream (new Netchannels.input_channel (open_in f)));;
> val readmessage : string -> Netmime.complex_mime_message = <fun>
> # let ss = readmessage "/tmp/aa" ;;
> val ss : Netmime.complex_mime_message =
>   (<obj>,
>   `Parts
>     [(<obj>, `Body <obj>); (<obj>, `Body <obj>); (<obj>, `Body <obj>);
>      (<obj>, `Body <obj>); (<obj>, `Body <obj>)])
> # let a,b = ss;;
> val a : Netmime.mime_header = <obj>
> val b : Netmime.complex_mime_body =
>   `Parts
>     [(<obj>, `Body <obj>); (<obj>, `Body <obj>); (<obj>, `Body <obj>);
>      (<obj>, `Body <obj>); (<obj>, `Body <obj>)]
> 
> What I want to do is to save each of the "Parts" in "b" in a seperate
> file.  But I don't know how to proceed to get the contents of the
> list.  In other words how do I get the tuples in "b" as strings? 

The core of a solution for your problem:

let parts = 
  match b with `Parts l -> l | _ -> failwith "No attachments found";;

List.iter
  (function
     (head, `Body body) ->
        let ch_rd = body # open_value_rd() in
        let ch_wr = new Netchannels.output_channel (open_out "filename") in
        ch_wr # output_channel ch_rd;
        ch_wr # close_out();
        ch_rd # close_in();
    | _ ->
        (* Nested multipart message *)
        ()
  )
  parts

It is assumed that you are only interested in the bodies of the parts,
and not in the headers (otherwise one would use write_mime_message to
create the files). Of course, there is still the problem how to get the
filenames, which is a constant here.

Last but not least, there is the phenomenon of nested multipart
messages. By default, read_mime_message performs a deep analysis of the
message, and returns nested parts as nested values. You can change that
with the argument ~multipart_style:`Flat, which leaves inner multipart
parts unparsed.

Hope this helps,

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2004-01-21 15:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-20 13:12 Johann Spies
2004-01-21 15:21 ` Gerd Stolpmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1074698477.3580.58.camel@ares \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=jspies@sun.ac.za \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox