8

I'd like to split my inbox into separate files (one file per one message) by bash command, or may be simple program in Java. How can I do it?

WBR, Thanx.

Andrey
  • 162
  • 1
  • 6
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
  • if you're using mbox, [this](http://www.qmail.org/man/man5/mbox.html) can help. and also [this](http://linuxcommand.org/man_pages/formail1.html) – hamed Jul 01 '12 at 12:25

3 Answers3

13

Just use formail. formail is a program that can process mailbox, run some actions for each message in the mailbox, separate messages and so on.

More info: http://www.manpagez.com/man/1/formail/

If you want just split a mailbox to separate files, I would suggest such solution:

$ cat $MAIL | formail -ds sh -c 'cat > msg.$FILENO'

From man:

   FILENO
        While splitting, formail  assigns  the  message  number  currently
        being  output  to  this  variable.   By presetting FILENO, you can
        change the initial message number being used and the width of  the
        zero-padded  output.   If  FILENO is unset it will default to 000.
        If FILENO is non-empty and does not contain a number, FILENO  gen-
        eration is disabled.

Note: formail is also included in procmail - https://github.com/BuGlessRB/procmail .

rushi
  • 142
  • 1
  • 12
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • how would you do this using bash and formail when you want to split not per mail but rather per filesize so that a maximum of n mb is generated per split? – Alex Jan 23 '15 at 12:36
  • 3
    `formail` is part of procmail on my machine (`apt-get install procmail`). – Att Righ Jan 29 '17 at 21:27
  • `formail` seems to be very primitive. It appears to split the file simply based on lines that start with `From: `. I have emails that contain that phrase at the beginning of a line in the email body, and `formail` breaks those emails in two rather than treat it as a single message. The `git` solution in Andrey's answer works much better for me. – Stephen Ostermiller Feb 13 '20 at 18:54
  • 5
    For me, this didn't work; it just built a single `msg.000` file that was identical to the mbox. – ki9 Jan 28 '21 at 00:57
7

The old mailbox files I have seen have messages separated by a line starting with "From ", followed by:

  • either "???@??? " and a date for old Eudora files:
    From ???@??? Fri Oct 16 10:49:27 1998
  • or just "- " and a date for Thunderbird files:
    From - Tue Jul 31 13:23:45 2007

So you can use this Perl oneliner

perl -pe 'open STDOUT, ">out".++$n if /^From (-|\?{3}\@\?{3}) /' < $IN

or, to have 6 digit 0-padded numbers (if your mailbox is less than 1m messages.) and an ".eml" extension:

perl -pe 'open STDOUT, sprintf(">m%06d.eml", ++$n) if /^From (-|\?{3}\@\?{3}) /' < $IN
mivk
  • 13,452
  • 5
  • 76
  • 69
1

there is also a specialized git command for this:

mkdir messages
git mailsplit -omessages mbox
mop
  • 347
  • 3
  • 9