2

I would like to know how to modify the below code to strip =20 characters at the end of many lines, and mainly to sort the messages chronologically from first received or sent to last. I am not sure if this would be an internal Perl routine or not.

#!/usr/bin/perl
use warnings;
use strict;
use Mail::Box::Manager;

my $file = shift || $ENV{MAIL};
my $mgr = Mail::Box::Manager->new(
    access          => 'r',
);

my $folder = $mgr->open( folder => $file )
or die "$file: Unable to open: $!\n";

for my $msg ($folder->messages)
{
    my $to          = join( ', ', map { $_->format } $msg->to );
    my $from        = join( ', ', map { $_->format } $msg->from );
    my $date        = localtime( $msg->timestamp );
    my $subject     = $msg->subject;
    my $body        = $msg->body;

    # Strip all quoted text
    $body =~ s/^>.*$//msg;

    print <<"";
From: $from
To: $to
Date: $date
$body

}

When trying to run this I get the following errors:

"my" variable $msg masks earlier declaration in same scope at x.pl line 16. syntax error at x.pl line 15, near ") ) " syntax error at x.pl line 31, near "}" (Might be a runaway multi-line << string starting on line 25) Execution of x.pl aborted due to compilation errors.

I am not sure as to why, as the syntax seems fine.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Joshxtothe4
  • 4,061
  • 10
  • 53
  • 83
  • Duplicate of http://stackoverflow.com/questions/368003/how-can-i-reorder-an-mbox-file-chronologically – Bombe Dec 15 '08 at 13:03
  • no, this is for perl, and more specific –  Dec 15 '08 at 13:03
  • Did you read my comment to your question in http://stackoverflow.com/questions/356763 about using sort { $a->timestamp <=> $b->timestamp } to change the ordering in the for loop? When I cut-n-paste the code above it works fine for me with no errors. – Hudson Dec 22 '08 at 21:50

2 Answers2

3

I guess that those instances of =20 are in the body of the message. Reading just a bit of the documentation for Mail::Message will reveal this helpful note:

BE WARNED that this returns you an object which may be encoded: use decoded() to get a body with usable data.

Thus instead of calling $msg->body in your loop, simply call $msg->decoded->string.

Accomplishing sorting should be easiest when you use Mail::Message::timestamp:

...
for my $msg ( sort { $a->timestamp <=> $b->timestamp } $folder->messages) )
...
innaM
  • 47,505
  • 4
  • 67
  • 87
  • Thanks for your answer. I am unsure how to make use of decoded() however –  Dec 15 '08 at 13:14
  • This is workingbeautifully, however I can not open the file with gedit. Is there a way to assign a character set to the file being written to? –  Dec 15 '08 at 13:55
0

I'd suggest to look at the MIME::Base64 module which includes MIME::QuotedPrint::Perl module to decode QP bodies.

Keltia
  • 14,535
  • 3
  • 29
  • 30