I am using the program below to sort and eventually print out email messages. Some messages may contain attachments or HTML code, which would not be good for printing. Is there an easy way to strip attachments and strip HTML but not the text formatted by HTML from the messages?
#!/usr/bin/perl use warnings; use strict; use Mail::Box::Manager; 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; # Strip all quoted text $body =~ s/^>.*$//msg; print MYFILE <<''; From: $from To: $to Date: $date Subject: $subject \n $body }
Mail::Message::isMultipartwill tell you whether a given message has any attachments.Mail::Message::partswill give you a list of the mail parts.Thus: