Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.)

In reply to RE: Re: How to parse emails with and without attachments? by le
in thread How to parse emails with and without attachments? by le

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found