Question

I am trying to use the following code, which I have not been able to test yet, because I get the following errors:

#!/usr/bin/perl
use warnings;
use strict;
use Text::Wrap;
use Mail::Box::Manager;
use HTML::Obliterate qw(extirpate_html);


open (MYFILE, '>>data.txt');
binmode(MYFILE, ':encoding(UTF-8)');


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 ( sort { $a->timestamp <=> $b->timestamp } $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->decoded->string;


if ( $msg->isMultipart ) {
    foreach my $part ( $msg->parts ) {
        if ( $part->contentType eq 'text/html' ) {
          my $nohtml = extirpate_html( $msg );
$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body
        }

else {


$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

}

}}

All the braces seem to match up, so I am unsure what the problem is

syntax error at x.pl line 46, near "else"
  (Might be a runaway multi-line << string starting on line 36)
Missing right curly or square bracket at x.pl line 63, at end of line
syntax error at x.pl line 63, at EOF
Execution of x.pl aborted due to compilation errors.

edit:

it now works, but the html is not striped: instead a few emails with stuff like
>
> interlaced throughout, causing it to be many more pages than it should. Is there a better way to do this

Was it helpful?

Solution

So line 36 seems to be

print MYFILE wrap("", "", <<"");

which means perl will wrap the following text until there is terminator "" (I never use confusing item like this, I always use END or UNTIL_END for simplicity.)

That terminator is then found on line 45 (the empty line), meaning next thing it processes is line 46:

else {

which doesn't make sense, since the previous if hasn't closed yet (the line 44 which has } is before the terminator "" so its treated as text for wrapping. Perl notices this and kindly suggest you this might be the culprit:

(Might be a runaway multi-line << string starting on line 36)

You need to swap lines 44 and 45 to first have terminator "" (empty line), then close the if with }. The second wrap in your example does this correctly.

OTHER TIPS

Answer to your modified question:

Instead of extirpating the message body, you extirpate the whole message instead. And then you don't use it anywhere.

my $nohtml = extirpate_html( $msg );
$body =~ s/^>.*$//msg;
$Text::Wrap::columns=80;
print MYFILE wrap("", "", <<"");
\n
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

Perhaps you need to change it to:

my $nohtml = extirpate_html( $body );
$nohtml =~ s/^>.*$//msg;

and then apply the $nohtml as the message body for wrap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top