From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA26239; Wed, 21 Jan 2004 16:21:24 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA26282 for ; Wed, 21 Jan 2004 16:21:23 +0100 (MET) Received: from mail.npc.de (fw.npc.de [62.225.140.214]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id i0LFLMv13892 for ; Wed, 21 Jan 2004 16:21:22 +0100 (MET) Received: from styx.ffm.npc.de (lion.npc.de [192.168.129.1]) by mail.npc.de (Postfix) with ESMTP id 152C1119B; Wed, 21 Jan 2004 16:21:19 +0100 (CET) Received: from hera.ffm.npc.de (hera.ffm.npc.de [192.168.130.8]) by styx.ffm.npc.de (Postfix STYX NPC GmbH) with ESMTP id 0D2C6C437; Wed, 21 Jan 2004 16:21:18 +0100 (CET) Received: from ares.ffm.npc.de (ares.ffm.npc.de [192.168.130.32]) by hera.ffm.npc.de (Postfix HERA NPC GmbH) with ESMTP id BE2C3159C9; Wed, 21 Jan 2004 16:21:17 +0100 (CET) Received: from ares.ffm.npc.de (ares.ffm.npc.de [192.168.130.32]) by ares.ffm.npc.de (Postfix ARES NPC GmbH) with ESMTP id 76247183D; Wed, 21 Jan 2004 16:21:17 +0100 (CET) Subject: Re: [Caml-list] Netmime: splitting email attachments From: Gerd Stolpmann To: Johann Spies Cc: caml-list@inria.fr In-Reply-To: <20040120131231.GE22271@sun.ac.za> References: <20040120131231.GE22271@sun.ac.za> Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.8 Date: 21 Jan 2004 16:21:16 +0100 Message-Id: <1074698477.3580.58.camel@ares> Mime-Version: 1.0 X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 netmime:01 gerd:01 stolpmann:01 2004:99 johann:01 spies:01 beginners:01 netmime:01 netstream:01 netchannels:01 val:01 val:01 failwith:01 netchannels:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 = > # let ss = readmessage "/tmp/aa" ;; > val ss : Netmime.complex_mime_message = > (, > `Parts > [(, `Body ); (, `Body ); (, `Body ); > (, `Body ); (, `Body )]) > # let a,b = ss;; > val a : Netmime.mime_header = > val b : Netmime.complex_mime_body = > `Parts > [(, `Body ); (, `Body ); (, `Body ); > (, `Body ); (, `Body )] > > 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