I have a requirement to receive incoming messages, process them in Perl and then dump an output into a file on disk. The incoming messages should always have a single nested email attached to them, which is what I am actually interested in.
i have a mime entity @entity
Originally i tried a very manual approach:
my @parts = $entity->parts;
my $numParts = $entity->parts;
my $copy;
my $parser = new MIME::Parser;
if ($numParts > 0){
foreach my $part (@parts){
my $type = $part->mime_type;
if (lc($type) eq "message/rfc822") {
$copy = $parser->parse_data($part->stringify_body);
$copy->head->replace('x-procserver', 'server123');
}
}
}
open (FFH, '>', 'test.eml');
print FFS $copy->stringify;
close(FFH)
The issue with this is something about the stringify_body
is bricking HTML text. It is copying the quoted-printable encoding = symbols at the end of each line, which interferes with HTML.
I have seen that MIME::Parser
has a 'extract_nested_messages' function, that can be used with 'REPLACE' to essentially remove my requirement for the foreach
loop checking mime_types. However I don't think people usually call the MIME::Parser function when they already have a MIME::Entity, as getting an entity is the main point of the parser. I don't control the original parsing event in the perl-based tool I am using, so how would be best to use this function?
Whilst it is also a really minor thing, that I have debated not doing, just to make things easier... i would quite like to add some headers to the attached message file before dumping it into a new file. I have tried quite a few things to do this, but most see the attachment message file as a single body, so modifying any header values on it just adds a new header section on top of the 'body' that already contains one.
If anyone is knowledgeable in MIME::Tools,Entity,Parser - i would appreciate some assistance.