Solved

More MIME-tools.

Posted on 2002-05-31
3
625 Views
Last Modified: 2008-03-03
I would like to use mimetools to parse a message into ram, look at the attached file names, then be able to save given parts (decided by type or recommended filename) to disk. I also want to be able to give each part a new name if the file already exists.

My code as of now is below, so how do i manipulate the files _before_ actually writing them to disk?

#!/usr/bin/perl -w
use strict;
$|++;

use MIME::Parser;
use MIME::Head;

my $parser = new MIME::Parser;
$parser->output_dir("./out");

# read STDIN
my $entity = $parser->read(\*STDIN) or die "couldn't parse MIME stream";

# to get message headers:
my $head = $entity->head;
$head->decode;
#print $head->as_string;
print "\n\n";
my $to = $entity->head->get('To');
my $from = $entity->head->get('from');
my $subject = $entity->head->get('subject');
my $xat = $entity->head->get('X-Apparently-To');
chomp ($to, $subject, $from, $xat);

print "Group: $xat Subject: $subject From: $from\n";
# message parts
my $part;
foreach $part($entity->parts) {
 # do something with message part

 # prints path to extracted file
 print $part->bodyhandle->path;
 print " ";

 # prints mime-type
# print $part->head->mime_type;
# print "\n";
}
print "\n======================================================================================================================\n";
0
Comment
Question by:j2
  • 2
3 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 7050884
Hi,

I had the same problems :)

As a result, I allow to MIME parser save attachments to temporary derectory and then in the cycle
foreach $part($entity->parts) {

# procedure SuggestNewPath takes temporary file path as
# parameter, extracts pure filename, checks if this name
# is free in target directory. If not, it adds number to
# the filename

$newpath = &SuggestNewPath($part->bodyhandle->path);

# after that "$newpath" contains new path to the file
# and I move old file to the new location

move ($part->bodyhandle->path, $newpath);

}

sub SuggestNewPath {
        my ($cfn) = @_;

        my ($name, $ext);
        my $basepath = "/target/directory"

        # get pure filename (assume that it's string
        # after last slash or backslash)
        $cfn =~ s/.*(\/|\\)//g;

        if (-e "$basepath/$cfn") {
                # if file with this name exists
                # we have to give different name
                # to the new file. Split file name
                # into 2 parts: filename & extension
                if ($cfn =~ /^(.*)\./) {
                        ($name,$ext) = ($1,$');
                } else {
                        ($name,$ext) = ($cfn,"");
                }
                my $i = 0;

                # trying filenames like
                # file2.ext, file3.ext

                while (-e "$basepath/$name".++$i.".$ext") {}
                return "$basepath/$name".$i.".$ext";
        } else {
                return "$basepath/$cfn";
        }
}
0
 
LVL 12

Author Comment

by:j2
ID: 7051000
Uhm, call me dumb here, but how would i implement that in a "complete" solution? I mean, it solves the generate-new-name part in an excellent way.

I cant make headfs or tails of the mime-tools docs, i seem to be missing the "Newbie" stuff/examples as in "how to check type, size, whatever" and stuff :-/
0
 
LVL 6

Accepted Solution

by:
andreif earned 200 total points
ID: 7051767
Hi,

feel free to ask more questions if you need. When I tried MIME-Tools for the first time, it was not a piece of cake :)

MIME-Tools is a set of modules, basic is MIME::Entity which contains MIME::Head, MIME::Body

To get more info about them, follow this link
http://ls6-www.cs.uni-dortmund.de/ir/projects/SFgate/CPAN/

and search for "MIME" (by name)

Start with
"MIME-tools - modules for parsing (and creating!) MIME entities"

Then check MIME::Entity, MIME::Head - there are some examples.

Feel free to ask me if you need help (don't close this question till you get it working)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now