tags:

views:

292

answers:

2

Hello, 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.

A: 

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

Keltia
+4  A: 

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
Thanks for your answer. I am unsure how to make use of decoded() however
Joshxtothe4
I edited my answer accordingly.
innaM
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?
Joshxtothe4