Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl: the Markov chain saw
 
PerlMonks  

Re: How to parse emails with and without attachments?

by Punto (Scribe)
on Jun 06, 2000 at 15:03 UTC ( [id://16641]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How to parse emails with and without attachments?

Should I post the parser code here? (It's rather long, so I don't want to post it now.)

I'm writting the exact same thing (for a "free webmail" program), I used this MIME Attachment Extractor code I found on the Snippets section, but I'd be interested on looking at your code (the snippet is perfect, but it wasn't _exactly_ what I needed).

  • Comment on Re: How to parse emails with and without attachments?

Replies are listed 'Best First'.
RE: Re: How to parse emails with and without attachments?
by le (Friar) on Jun 06, 2000 at 16:50 UTC
    Here's the code:
    #!/usr/bin/perl -w use MIME::Parser; use DBI; use strict; my $database = "xxxxxxx"; my $dbuser = "xxxxxxx"; my $dbpasswd = "xxxxxxx"; my $outputdir = "/some/directory"; my $parser = new MIME::Parser; $parser->output_dir($outputdir); $parser->output_prefix("attachment"); $parser->output_to_core(); my $entity = $parser->read(\*STDIN); my $msg_header = $entity->stringify_header; my $num_parts = $entity->parts; my $msg_body; if ($num_parts > 0) { for (my $i = 0; $i < $num_parts; $i++) { my $part = $entity->parts($i); my $type = $part->mime_type; my $bh = $part->bodyhandle; if ($type =~ m/text/) { if (my $io = $part->open("r")) { while (defined($_ = $io->getline)) { $msg_body .= $_; } $msg_body .= "\n\n"; $io->close; } my $status = system("rm '$bh->{MB_Path}'"); die $! unless $status == 0; } else { my $file = $bh->{MB_Path}; my $oldfile = $file; $file =~ s/$outputdir\///; my $now = time(); $file = $now . "-" . $file; my $newfile = $outputdir . "/" . $file; my $status = system("mv '$oldfile' '$newfile'"); die $! unless $status == 0; $file = "some/directory" . $file; $file = "http://url/" . $file; $msg_body .= "ATTACHMENT: $file\n"; } } } else { if (my $io = $entity->open("r")) { while (defined($_ = $io->getline)) { $msg_body .= $_; } $msg_body .= "\n"; $io->close; } $entity->purge; } ### The rest is just database processing ...
    The problem is that the parser dies on some mails when it should remove those temporary files.

    (I know this is dirty code, but I made it most out of trial-and-error.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16641]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.