Issue
I have an HTML and TEXT Mime message. I would like to delete the HTML part. It looks like this will do the trick:
From MIME::Entity
:
### Delete some parts of a multipart message:
my @keep = grep { keep_part($_) } $msg->parts;
$msg->parts(\@keep);
But, I'm not sure how to read this, or really what to call it (callback?).. I can locate the part as follows:
for my $part ($msg->parts()) {
if ($part->mime_type eq 'text/html') {
Solution
When you supply an ARRAYREF
of MIME::Entity
s to the parts
function, it sets the object to contain exactly those MIME::Entity
s. All previous entities are dropped.
The @keep
array in the example contains those MIME::Entity
s that for which the keep_part
returned true and $msg->parts(\@keep);
is what sets the new MIME::Entity
s in $msg
.
The implementation of keep_part
could therefore be something like this:
sub keep_part {
shift->mime_type ne 'text/html';
}
That is, it'll return true if the mime type for the supplied argument is not text/html
.
If the condition isn't more complex than that, you may want to just filter it inline instead:
# create an ARRAYREF to the parts to keep and set the parts in $msg:
$msg->parts([ grep { $_->mime_type ne 'text/html' } $msg->parts]);
After the filtering has been done, you do not need to check if ($part->mime_type eq 'text/html')
in your loop anymore. All text/html
MIME entities will have been removed:
for my $part ($msg->parts) {
print $part->mime_type . "\n"; # no text/html
}
Answered By - Ted Lyngmo Answer Checked By - Robin (WPSolving Admin)