#!/usr/bin/perl
#
# Wolfgang Rosner, wrosner@tirnet.de
# oct 2016
# try to extract header and HTML from claws mail files
# and pipe them to pdf generator
# preliminary draft
# don't expect anything serious from this hack
# - only errors and bullshit -
use strict;
use warnings;
use Email::MIME;
# use Email::Simple;
use Data::Dumper;
# ======== some config here: ===========
# list of headers to print
my @headlist = qw(From To cc bcc Date Subject);
# find your favourite pdf viewer
my $pdfviewer = `which okular` or die ("pdfviewer not found");
my $wh2p = `which wkhtmltopdf` or die ("wkhtmltopdf not installed");
# ============= end of config - no user change below =============
# read message from STDIN
my $message = "";
while (<>) {
$message .= $_
}
print "### reading done ###\n";
my $parsed = Email::MIME->new($message);
my @parts = $parsed->parts; # These will be Email::MIME objects, too.
# my $decoded = $parsed->body;
# my $non_decoded = $parsed->body_raw;
my $content_type = $parsed->content_type;
printf "## parsing done ##\n";
printf "content-type: %s \n", $content_type ;
print "=== debug structure: ===\n";
print $parsed->debug_structure;
print "=====================\n";
# collect header lines according to configured @headlist
my @hl_str = ();
my $headitem;
foreach $headitem (@headlist) {
my @headtxl = $parsed->header($headitem) ;
my $htlitem ;
foreach $htlitem ( @headtxl) {
push @hl_str , sprintf ("%s: %s", $headitem, $htlitem) ;
}
}
# print Dumper ( \@hl_str);
# produce some basic html
my $htmlheadstr = "