Category: mail utils
Author/Contact Info Artem Litvinovich
Description: msg2mbox README. Author: Artem Litvinovich msg2mbox convert MDaemon formatted email files into unix standard mbox format.
#!/usr/bin/perl
#
# MDaemon msg to mailbox mail file converter
# Usage: msg2mbox mail.msg
# Suggested use: ls -c1 *.msg | awk '{system("msg2mbox "$1)}' >> mail.
+mbox
#
# msg2mbox utility by Artem Litvinovich 


use strict;
die "Must specify msg file.\nUsage: msg2mbox file.msg > file.mbox\n" u
+nless($ARGV[0]);

open(MSG,"<$ARGV[0]") || die("Could not open");
my($adr,$dt) = ();
while(my $line = <MSG>) {
    chomp($line);
    my($fld,$nfo) = split(':',$line,2);
    $adr = $nfo if($fld =~ /^Return-path/);
    $dt = $nfo if($fld =~ /^Date/);
    $adr = "__SKIP" if($line =~ /^Encrypted\s+By\s+MDaemon/);
    last if(($adr eq "__SKIP") or ($adr and $dt));
}

#filter encrypted messages
if($adr eq "__SKIP") {
    close(MSG);
    exit 1;
}

#filter out msft junk
$adr = join ' ',split /\s/,$adr;
$dt = join ' ',split /\s/,$dt;

#kill spaces
$adr =~ s/\s//;

#kill surrounding brackets
$adr =~ s/<(.+)>/$1/;

#kill outside spaces
$dt =~ s/^\s*//;

#kill commas
$dt =~ s/,//;

#set dummy vals if info not found
$adr = "unknown\@uknown.com" if(!$adr);
$dt = "Mon, 1 Jan 2000 01:00:00" if(!$dt);

#reformat date
my($dw,$dat,$mo,$yr,$tm,$adj) = split(/\s/,$dt);
$dt = "$dw $mo $dat $tm $yr";

print "From $adr $dt\n";
seek(MSG,0,0);
while(my $line = <MSG>) {
    $line =~ s/\r//g; #kill ^M
    print $line;
}
close(MSG);
Replies are listed 'Best First'.
•Re: msg2mbox converter
by merlyn (Sage) on Feb 28, 2003 at 11:53 UTC
    While this is fine for a standalone program, for scalability, you may wish to consider the Mail::Box family of modules. It's being actively maintained (in fact, the latest release was yesterday as I write this), and would probably have reduced your program to three lines.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.


    update: I may have posted a bit too soon. It appears you are translating from a non-standard message format that might not be supported by standard tools, in which case most of your program would not go away. My apologies. However, dealing with the Mbox format on the output could still benefit from using the module, so that others can reuse this in places that aren't using Mbox. I had never heard of MDaemon before, and didn't realize the unstandard the message format.
Re: msg2mbox converter
by Aristotle (Chancellor) on Mar 01, 2003 at 16:33 UTC
    Why not use Perl's tools?
    #!/usr/bin/perl -w use strict; # MDaemon msg to mailbox mail file converter # Usage: msg2mbox *.msg > mail.mbox die "usage: msg2mbox *.msg > file.mbox\n" unless @ARGV; my @file = @ARGV; FILE: for(@file) { @ARGV = $_; my @line = <>; my ($addr, $date); for(@line) { s/\r//g; if /^Encrypted\s+By\s+MDaemon/) { next FILE; } elsif(/^Return-path:(.*)/) { local $_ = join ' ', split " ", $1; tr/ //d; s/<(.+)>/$1/; $addr = $_; } elsif(/^Date:(.*)/) { local $_ = join ' ', split " ", $1; tr/,//d; # weekday day month year time adj -> weekday month day tim +e year $date = join " ", +(split)[0, 2, 1, 4, 3]; } } $addr ||= "unknown\@uknown.com"; $date ||= "Mon Jan 1 01:00:00 2000"; print "From $adr $dt\n", @line; }

    Untested.

    You should probably add a Lines: or Content-length: header if neither is present - or just let Mail::Box handle the gory details for you.

    Makeshifts last the longest.