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

Detachments II: The Sequel

by hacker (Priest)
on Nov 09, 2003 at 20:57 UTC ( [id://305713]=perlquestion: print w/replies, xml ) Need Help??

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

I decided to pick up a script I wrote awhile ago, and get it back on track. I wrote a little about it in Dealing with "Detachments". Here's the issue:

The script is an alias in /etc/mail/aliases, so that incoming mail sent to this address, is sent to my script and processed. Originally, the script required that the key=value pairs in this script had to occur in the body of the message itself, in a template I parse with Config::General. I've added the capability to attach the template to the message itself as an attachment (easier for Outlook/Windows users, apparently). So far, so good.

What I'm trying to do now, is "detach" that attachment, store it in a scalar I can manipulate (or even write it to disk, for the moment. I can deal with IO::Scalar on it later to omit the disk hit). When I detach the file, using MIME::Parser, I have a few options, each with their own confusing quirks:

  • Use the output_dir() function, which will write the message to the specified directory, prefixed with "msg" in front of the filename. Oddly, when I do this, I get "msg-$$-1.txt" and "msg-$$-2.extension", where the "$$" is the PID of the process. The "msg-$$-1.txt" file is ALWAYS 0-bytes in size, and the "msg-$$-2.extension" is the exact file that was attached to the message. This means, if I attach 'File.doc' to the message, I get "msg-12345-1.txt" (0-bytes), and "msg-12345-2.doc" (the doc file which was attached).
  • I can also use output_under(), which will create a directory of "msg-56754345678-1" or some seemingly random sequence of numbers, and under that, "msg-$$-1.txt" (now 1-byte, a space), and "msg-$$-2.extension" (my target attachment file).
  • There is also ignore_filename(1), which I can use to try to override the filename, but I can't for the life of me figure out how to provide the REAL attachment filename.

The code I'm using looks like this:

my $md5file = md5_hex($date); my $workpath = "/var/lib/pler"; my $date = UnixDate("today","%b %e, %Y at %T"); my $parser = new MIME::Parser; $parser->output_dir("$workpath"); $parser->output_prefix("$md5file"); my $filer = $parser->filer; $filer->ignore_filename(0); my $entity = $parser->read(\*STDIN);

What I'd like, ideally, is to have the attachment "detached", and named as "$md5file-originalfile.extension", so I can then manipulate it and handle it as a normal file or file-handle-in-a-scalar.

  1. How do I get the original filename back when detaching?
  2. If I'm using output_under(), how do I know what that long string of numbers (it's not the PID) in the directory name is?
  3. Why do I see those *.txt files as 0-byte or 1-byte files? What is their purpose, if the "real" attachment is in a separate file all its own?
  4. Is there another (better/more-suited) module for this task?
  5. I'm also using Mail::Internet to split the message envelope up so I can get the "From:", "To:" "Subject:" and body out of the message. Should I use MIME::Parser for all of these in one shot? Or should I use both?

Color me confusigated.

Replies are listed 'Best First'.
Re: Detachments II: The Sequel
by Anonymous Monk on Nov 10, 2003 at 09:32 UTC
    1. Hints for a name are in Content-Disposition and sometimes Content-Type fields under various attribute names. However, there are many security risks with using these names. However, if you want to unpack HTML messages with images automatically, you will need to use these names.
    2. ...
    3. Don't know for sure, but probably the content of the preamble and epilogue are written to a separate file as well.
    4. You can do this with Mail::Box
      use Mail::Message; use File::Temp; my $dir = tempdir; mkdir $dir or die; foreach my $p ($message->parts) { my $fn = $part->dispositionFilename($dir); $part->decoded->write(filename => $fn) or die "Couldn't write to $fn: $!\n"; }
    5. Yes

    20031110 Edit by Corion: Changed PRE to CODE tags, closed list

      Script in (4) lacks one line:
      my $message = Mail::Message->read(\*STDIN);
      
      See Mail::Box-Index for additional features.
      Mark Overmeer (who is waiting for an entrance password for the Monastry)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://305713]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-18 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found