in reply to Re: eMail processing for alarmistic
in thread eMail processing for alarmistic

Since your reply i complemented my code a little bit, found a few problems, searched a bit more and composed a solution based on other's code too.

So if this may help other newcomers i'm posting the code i've copy/pasted and built so far.

This program accepts a outlook .msg file as an argument and prints the readable text parts of the email i need

#!/usr/bin/perl -w use strict; use warnings; use Email::MIME; use Email::Address::XS; use Encode; use Email::Outlook::Message; for my $filename ( glob("$ARGV[0]*") ) { # will create an Email::Mime object from .msg file my $msg = new Email::Outlook::Message $filename or die "Can't ope +n $filename: $!\n"; my $msg_mime = $msg->to_email_mime; #print "Mime parts: " . $msg_mime->as_string, "\n"; my ($from) = Email::Address::XS->parse($msg_mime->header ('From')) +; my ($to) = Email::Address::XS->parse($msg_mime->header ('To')); my $subject = encode ("utf8", $msg_mime->header ('Subject')); my $date = $msg_mime->header ('Date'); print "FROM: ", $from, "\n"; print "TO: ", $to, "\n"; print "SUBJECT: ", $subject, "\n"; print "DATE: ", $date, "\n\n"; ## tell the filename reading # print 'Filename: ', $filename, "\n"; my (@mailData, $body); $msg_mime->walk_parts(sub { my ($part) = @_; # warn($part->content_type . ": " . $part->subparts); if (($part->content_type =~ /text\/plain; charset=\"?utf-8\" +?/i) && !@mailData) { @mailData = split( '\n', $part->body); } elsif (($part->content_type =~ /text\/plain; charset=\"?us-a +scii\"?/i) && !@mailData) { @mailData = split( '\n', $part->body); } elsif (($part->content_type =~ /text\/plain; charset=\"?wind +ows-1252\"?/i) && !@mailData) { @mailData = split( '\n', $part->body); } elsif (($part->content_type =~ /text\/plain; charset=\"?iso- +8859-1\"?/i) && !@mailData) { @mailData = split( '\n', $part->body); } }); #print $part->body; #only need utf-8 for this test foreach my $line (@mailData) { print encode ("utf8", $line) . "\n"; } }