Debian Codegraph @

script/msgconvert

raw
1 #!/usr/bin/perl -w
2 #
3 # msgconvert:
4 #
5 # Convert .MSG files (made by Outlook (Express)) to multipart MIME messages.
6 #
7
8 use Email::Address;
9 use Email::Outlook::Message;
10 use Email::Sender::Transport::Mbox;
11 use Getopt::Long;
12 use Pod::Usage;
13 use File::Basename;
14 use vars qw($VERSION);
15 $VERSION = "0.921";
16
17 # Setup command line processing.
18 my $verbose = '';
19 my $mboxfile = '';
20 my $outfile = '';
21 my $help = ''; # Print help message and exit.
22 GetOptions(
23 'mbox=s' => \$mboxfile,
24 'verbose' => \$verbose,
25 'outfile=s' => \$outfile,
26 'help|?' => \$help) or pod2usage(2);
27 pod2usage(1) if $help;
28
29 # Check file names
30 defined $ARGV[0] or pod2usage(2);
31
32 my $using_mbox = $mboxfile ne '';
33 my $using_outfile = $outfile ne '';
34 if ($using_outfile && scalar @ARGV > 1) {
35 die "The --outfile parameter does not allow to specify more than one " .
36 "<file.msg>. See --help for more details.";
37 }
38 my $transport;
39
40 if ($using_mbox) {
41 $transport = Email::Sender::Transport::Mbox->new({ filename => $mboxfile });
42 }
43
44 foreach my $file (@ARGV) {
45 my $msg = new Email::Outlook::Message($file, $verbose);
46 my $mail = $msg->to_email_mime;
47 if ($using_mbox) {
48 my $from;
49 my @from_addr = Email::Address->parse($mail->header('From'));
50 $from = $from_addr[0]->address if @from_addr;
51 $transport->send($mail->as_string, { from => $from || '' });
52 } else {
53 if (!$using_outfile) {
54 my $basename = fileparse($file, qr/\.msg/i);
55 $outfile = "$basename.eml";
56 }
57 if ($outfile eq '-') {
58 open OUT, ">&", STDOUT;
59 } else {
60 open OUT, ">", $outfile or die "Can't open $outfile for writing: $!";
61 }
62 print OUT $mail->as_string;
63 close OUT;
64 }
65 }
66
67 #
68 # Usage info follows.
69 #
70 __END__
71
72 =head1 NAME
73
74 msgconvert - Convert Outlook .msg files to mbox format
75
76 =head1 SYNOPSIS
77
78 msgconvert [options] <file.msg>...
79
80 msgconvert --outfile <outfile> <file.msg>
81
82 Options:
83 --mbox <file> deliver messages to mbox file <file>
84 --outfile <oufile> write message to <outfile> or - for STDOUT
85 --verbose be verbose
86 --help help message
87
88 =head1 OPTIONS
89
90 =over 8
91
92 =item B<--mbox>
93
94 Deliver to the given mbox file instead of creating individual .eml
95 files.
96
97 =item B<--outfile>
98
99 Writes the message into the outfile instead of individual .eml files. For
100 STDOUT "-" can be used as outfile. This option cannot be used together with
101 multiple <file.msg> instances.
102
103 =item B<--verbose>
104
105 Print information about skipped parts of the .msg file.
106
107 =item B<--help>
108
109 Print a brief help message.
110
111 =back
112
113 =head1 DESCRIPTION
114
115 This program will convert the messages contained in the Microsoft Outlook
116 files <file.msg>... to message/rfc822 files with extension .eml.
117 Alternatively, if the --mbox option is present, all messages will be put in
118 the given mbox file. This program will complain about unrecognized OLE
119 parts in the input files on stderr.
120
121 =head1 BUGS
122
123 The program will not check whether output files already exist. Also, if you
124 feed it "foo.MSG" and "foo.msg", you'll end up with one "foo.eml",
125 containing one of the messages.
126
127 Not all data that's in the .MSG file is converted. There simply are some
128 parts whose meaning escapes me. One of these must contain the date the
129 message was sent, for example. Formatting of text messages will also be
130 lost. YMMV.
131
132 =head1 AUTHOR
133
134 Matijs van Zuijlen, C<[email protected]>
135
136 =head1 COPYRIGHT AND LICENSE
137
138 Copyright 2002--2020 by Matijs van Zuijlen
139
140 This program is free software; you can redistribute it and/or modify
141 it under the same terms as Perl itself.
142
143 =cut