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.
- How do I get the original filename back when detaching?
- 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?
- 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?
- Is there another (better/more-suited) module for this task?
- 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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.