Gossamer Forum
Home : General : Perl Programming :

Parsing an Incoming Email Using Perl

Quote Reply
Parsing an Incoming Email Using Perl
Hi Guys,

I'm trying to parse an email coming from <STDIN>. I need to parse the email and insert into a database.

The major problem I'm finding is trying to deal with all of the various mime-types. If it contains an attachment or is multipart/alternative, multipart/mixed or whatever then it's very complex to decode. In the end, I'd just to insert a text-only part of the email into the database. I've tried google (groups, various pages), cpan (Mime::Parse, Internet::Mail, etc), and various scripts from Hotscripts - none of which worked.


Does anyone know of some code that will allow me to extract the text only part of an email? If anyone can help it would be really appreciated.

Last edited by:

cwi: Jun 27, 2003, 12:04 PM
Quote Reply
Re: [cwi] Parsing an Incoming Email Using Perl In reply to
I use MIME::Parser

Code:
my $parser = MIME::Parser->new;

$parser->output_to_core(1);

my $entity = $parser->parse_data(\*STDIN);
my $body = $entity->bodyhandle->as_string;

You can get more involved by handling attachments etc. You can see the docs for that.

Last edited by:

Paul: Jun 27, 2003, 12:06 PM
Quote Reply
Re: [Paul] Parsing an Incoming Email Using Perl In reply to
Thanks Paul, I had given up on Mime::Parser until you said that you used it. Works great now. Cheers for your help!