Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29769

HTML email to plain text with MIME::Entity

$
0
0

I'm using a perl script to convert HTML mails to plain text.

The current code (for multipart mails) looks like this:

my $parser = new MIME::Parser;my $entity = $parser->parse(\*STDIN) or die "parse failed\n";for my $part ($entity->parts()) { if ($part->mime_type eq 'text/html') { my $bh = $part->bodyhandle; my $tree = HTML::TreeBuilder->new(); $tree->utf8_mode(); $tree->parse($bh->as_string); my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 72); my $txt = $formatter->format($tree); my $txtEntity=MIME::Entity->build(Data  => $txt,                                 Type  => "text/plain",                                 Encoding => "8bit"                                 ); $entity->add_part($txtEntity,0); }}$entity->print(\*STDOUT);

It works but it adds just adds the plain text part to the existing parts and doesn't replace the HTML part.
So I came up with this:

my $head = $entity->head;my $txtEntity=MIME::Entity->build(Data  => $txt,                               Type  => "text/plain",                               Encoding => "8bit",                               From    => $head->get('From',0),                               To      => $head->get('To',0),                               Subject => $head->get('Subject',0),                               Cc => $head->get('Cc',0)                               );$txtEntity->print(\*STDOUT);

But that could remove some parts of the email header.Is there a function to replace the HTML body completely with the plain text one?

Thanks!


Viewing all articles
Browse latest Browse all 29769

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>