How do I distinguish a mail header from a MIME header in Perl MIME::Tools?

Viewed 105

In MIME::Tools parsing an email gives you a MIME::Entity consisting of MIME::Head and MIME::Body. As per MIME::Head#NOTES email headers and MIME headers are considered the same thing.

For me these two are very different things:

  • An email header is something that contains "From:", "To:" and "Subject:". In this case the Body ist the non-attachment part of the message, something that gets displayed in a mail program.
  • a MIME header ist something that contains "Content-Type:", "Content-Disposition:" and "Content-Transfer-Encoding". In this case the Body is an attachment, which MAY be displayed in a mail program but typically gets stored somewhere through "Save as ...".

For the moment I assume that a MIME::Head is an email header and the parts contain MIME headers unless is_multipart is signaled: then MIME::head of that part is an email header.

But that seems a bit ... heuristic to me. Is there a better way to distinguish both?

Here is my current code:

use MIME::Parser;

$parser = new MIME::Parser;
dissect_message($parser->read(\*STDIN), 1);

sub dissect_message {
    $message=shift;
    $is_mail_header=shift;
    
    $body_name="BODY";
    if($head = $message->head) {
        if($is_mail_header) {
            # Output mail HEADER
        } elsif($head->recommended_filename) {
            # This is a MIME header
            $body_name=$head->recommended_filename;
        }
    }
    
    if($body = $message->bodyhandle) {
        open(BODY, $body_name)
        print BODY $body->as_string;
        close(BODY);
    }
    
    @p=$message->parts;
    for($i=0; $i <= $#p; $i++) {
        $p=$p[$i];
        dissect_message($p, $p->is_multipart);
    }
}
0 Answers
Related