use MIME::Parser; split_entity($entity); # $entity is a MIME::Entity object ################################################ sub split_entity { ################################################ local $entity = shift; my $num_parts = $entity->parts; # how many mime parts? if ($num_parts) { # we have a multipart mime message foreach (1..$num_parts) { split_entity( $entity->parts($_ - 1) ); # recursive call } } else { # we have a single mime message/part if ($entity->effective_type =~ /^text\/plain$/) { # text message handle_text($entity->bodyhandle->as_string); } else { # no text message handle_other($entity->bodyhandle->path); } } }