Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I am new to perl and i wonder if anyone could help me!?
I amtrying to make an mailreader program that reads the mail thrue Net::POP3 and store it in MySQL with DBI. When people sends mail with HTML it get ugly. How can I delete all the crap and only have the "raw" message after.

David

Replies are listed 'Best First'.
Re: How can I delete the MIME Headers
by mpolo (Chaplain) on Jun 06, 2001 at 18:25 UTC
    You will want to use MIME-tools, most likely. This is a heavy-duty module distribution, but it parses all the MIME stuff, with the caveat that it loads attachments into memory, so may die on giant MP3 files if you don't have a whole lot of memory.

    The documentation is good, but you have to flip back and forth a bunch to get what you need. Here is a basic usage summary. I assume your mail is saved to a file named "incoming".

    use MIME::Parser; my $parser=MIME::Parser->new; $parser->output_dir("/temp"); open FILE "incoming" || die "Couldn't open file\n"; my $entity=$parser->read(\*FILE)|| die "Couldn't parse\n"; my $head=$entity->head; $entity->dump_skeleton; ## Print out the structure.
    HTML messages are often sent as multipart/alternative, which means you can look through the parts to find one with Content-Type: text/plain. If there's not one there, you'll have to parse the HTML to get the message. As the other response here mentions, HTML::Parser will help you there.
Re: How can I delete the MIME Headers
by the_slycer (Chaplain) on Jun 06, 2001 at 18:11 UTC