Hello all,

I am looking to download all messages from an IMAP server (Exchange) from two folders to two files in mbox format. I have done this using Mail::IMAPClient; more specifically in the following manner:

foreach my $mbox qw( SPAM HAM ) { $imap->select($mbox); my @msgs = $imap->search("ALL"); $imap->message_to_file("/home/mail/mbox/$mbox".".msgs",@msgs); $imap->delete_message(@msgs); $imap->close(); }
This seems to work just fine except the files HAM.msgs and SPAM.msgs aren't in mbox format, they are just files with lists of messages in them. In an attempt to convert them, I am using the following command: `formail < /home/mail/mbox/HAM.msgs > HAM`. However, that leaves me with an mbox file that just has 1 long messgae according to mbox readers (however that long message contains all the messages in the file).

Therefore my question is threefold:

  1. Would it be better (or work for that matter) if I could somehow be using those pipes in $imap->message_to_file so the messages could be piped directly through formail avoiding an unnecessary step?
  2. Is there a Perl Module that would do this on the fly or do a better job than I appear to be doing it using system commands?
  3. Does someone have a better idea?

Ultimate goal: To download the 2 folders which contain HAM and SPAM messages, feed them through sa-learn (spamassassin which requires mbox format), convert them to Maildir format using mb2md (which I already have done) so I can have dspam_train learn them (because dspam requires Maildir format to learn).

UPDATE:

This is the code that eventually ended up doing exactly what I needed it to do. Thanks to everyone for all the help, it is greatly appreciated. I will be putting the final script up on my webpage next week (http://eric.lubow.org/projects), it will be the IMAP SPAM learning script for those interested:

use Mail::IMAPClient; my $imap = Mail::IMAPClient->new( Server => 'imap.server.com:14 +3', User => 'user', Password => 'pass') or die "IMAP Failure: $@"; foreach my $box qw( SPAM HAM ) { my $file = "/home/mail/mbox/". $box; $imap->select($box); my @msgs = $imap->search('ALL') or die "Couldn't get all messages\n"; foreach my $msg (@msgs) { open my $pipe, "| formail >> $file" or die("Formail Open Pipe Error: $!"); $imap->message_to_file($pipe, $msg); close $pipe or die("Formail Close Pipe Error: $!"); $imap->delete_message($msg); } # Now expunge the messages and close the folder $imap->expunge($box); $imap->close($box); } $imap->logout();

In reply to Downloading IMAP Folders to mbox file by madbombX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.