Search code examples
perlemailmimecpantemplate-toolkit

Parse and display MIME multipart email on website


I have a raw email, (MIME multipart), and I want to display this on a website (e.g. in an iframe, with tabs for the HTML part and the plain text part, etc.). Are there any CPAN modules or Template::Toolkit plugins that I can use to help me achieve this?

At the moment, it's looking like I'll have to parse the message with Email::MIME, then iterate over all the parts, and write a handler for all the different mime types.

It's a long shot, but I'm wondering if anyone has done all this already? It's going to be a long and error prone process writing handlers if I attempt it myself.

Thanks for any help.


Solution

  • It doesn't sound like a difficult job to me:

    use Email::MIME;
    my $parsed = Email::MIME->new($message);
    my @parts = $parsed->parts; # These will be Email::MIME objects, too.
    print <<EOF;
    <html><head><title>!</title></head><body>
    EOF
    for my $part (@parts) {    
        my $content_type = $parsed->content_type;
        if ($content_type eq "text/plain") {
             print "<pre>", $part->body (), "</pre>\n";
        }
        elsif ($content_type eq "text/html") {
            print $part->body ();
        }        
        # Handle some more cases here
    }
    print <<EOF;
    </body></html>
    EOF