MIME-tools help 
Author Message
 MIME-tools help

Anyone know how to use this module? Can I use this to
get access to parts of an email?  I would like to make a
simple call to a subroutine to get access to To, From,
Body, attachments etc...

Thanks.

John



Fri, 19 Jan 2001 03:00:00 GMT  
 MIME-tools help
[Posted and mailed]


Quote:

> Anyone know how to use this module? Can I use this to
> get access to parts of an email?  I would like to make a
> simple call to a subroutine to get access to To, From,
> Body, attachments etc...

> Thanks.

> John

Yes. I have been using them for about a month and a half now. They appeared
a little bit daunting to me (a relative newbie) at first but there are some
great example scripts that you can use to experiment with.  Also the pod
documentation in these packages is very comprehensive. The only problems
I have had are mistakes and assumptions I have made about MIME.  Basically
every MIME encoded message I can throw at them works, nuff said really.

One other point, unless the headers are coming MIME encoded (something that
makes me shudder) then any of the standard email modules will get the To and
From lines of thre header and the entire body of the message. If the body
is MIME encoded then you basically need to just dump (yes thats one of the
subs) the body through a script very similar to the mimeexplode example
script that comes with the module. This will decode the part(s) and store
them in memory or a file (returning a filehandle) for you to use.

From the command line it is very simple once you get the hang of using the
various packages, it's when you try to complicate things by getting the
output in a Browser friendly fashion that it becomes fun.

I have also just checked on CPAN and I think there is a new version out that
I will have to investigate.

--
Ian J. Garlick

Executive ability is deciding quickly and getting somebody else to do
the work.
                -- John G. Pollard



Sat, 20 Jan 2001 03:00:00 GMT  
 MIME-tools help
Thanks Ian.  The module seems to do alot.  I am having trouble figuring
out how to get access to the body.  I am forwarding the emails to my script
which MIME is parsing.

my $parser = new MIME::Parser;
mkdir("/tmp/$$",0755);
$parser->output_dir("/tmp/$$");

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

The method dump_skeleton works great.  I am assuming that the
text of the body will be writen to a filename "msg-<dir name>-1.doc".
I am not sure what "1" is suppose to represent in the filename.
The method also dumps the number of files it created.  I see this
reference as "Num-parts: #".  I could not find a method which
I can call to check for the total numbers of files it will output.
What would be helpful would be an array of files it created.

Once I have the body handle I can then print it out only if it does
not contain attachments.

    $bodyh = $entity->bodyhandle;
   $IO = $bodyh->open("r") || die "open body: $!";
   while (defined ($_ = $IO->getline)) {
      print $_;
   }
   $IO->close;

This does not work when there are attachments.  Do you have problems
with this?  I keep getting the message :
Can't call method "open" without a package or object reference at mime.pl line
63, <STDIN> chunk 245.

Thanks again for you help.

John

Quote:

> [Posted and mailed]



> > Anyone know how to use this module? Can I use this to
> > get access to parts of an email?  I would like to make a
> > simple call to a subroutine to get access to To, From,
> > Body, attachments etc...

> > Thanks.

> > John

> Yes. I have been using them for about a month and a half now. They appeared
> a little bit daunting to me (a relative newbie) at first but there are some
> great example scripts that you can use to experiment with.  Also the pod
> documentation in these packages is very comprehensive. The only problems
> I have had are mistakes and assumptions I have made about MIME.  Basically
> every MIME encoded message I can throw at them works, nuff said really.

> One other point, unless the headers are coming MIME encoded (something that
> makes me shudder) then any of the standard email modules will get the To and
> From lines of thre header and the entire body of the message. If the body
> is MIME encoded then you basically need to just dump (yes thats one of the
> subs) the body through a script very similar to the mimeexplode example
> script that comes with the module. This will decode the part(s) and store
> them in memory or a file (returning a filehandle) for you to use.

> From the command line it is very simple once you get the hang of using the
> various packages, it's when you try to complicate things by getting the
> output in a Browser friendly fashion that it becomes fun.

> I have also just checked on CPAN and I think there is a new version out that
> I will have to investigate.

> --
> Ian J. Garlick

> Executive ability is deciding quickly and getting somebody else to do
> the work.
>                 -- John G. Pollard



Sat, 20 Jan 2001 03:00:00 GMT  
 MIME-tools help
[Posted and mailed]


Quote:

> Thanks Ian.  The module seems to do alot.  I am having trouble figuring
> out how to get access to the body.  I am forwarding the emails to my script
> which MIME is parsing.

> my $parser = new MIME::Parser;
> mkdir("/tmp/$$",0755);
> $parser->output_dir("/tmp/$$");

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

Yep I do somrthing very similar to this maybe even exactly the same, I have
wrapped it up in a module so deciphering it to show you only the relevant
part is fun. (The module is an wrapper around various modules in MIME-Tools
which I use to encode an decode messages giving me output in the form I want
it. Yes this includes an array of parts so a count is easy) The following
is very similar to the above: (be kind I am still learning)

[not included:- the bits that set up these variables]
   my $entity  = {
      USER     => $user,
      TYPE     => {%TypeFor},
      DIR      => $dir,
      ENT      => undef,
      FILES    => [],  
   };

   my $obj = bless $entity, $self;

   if ($hdr) {
      my $msg = $hdr."\n".$bdy;
      (-d $dir) or mkdir $dir,0755 or
         die "Can't create directory $dir: $!";
      my $parser = new MIME::Parser;
      $parser->output_to_core($size);   # size should be 0, I set it earlier
      $parser->output_dir($dir) if $dir;
      $obj->{ENT} = $parser->parse_data($msg);
   }
[there is more to encode mime messages]

        return $obj;

As you can see I use the parser->parse_data call to get a decoded entity.

Quote:

> The method dump_skeleton works great.  I am assuming that the
> text of the body will be writen to a filename "msg-<dir name>-1.doc".
> I am not sure what "1" is suppose to represent in the filename.
> The method also dumps the number of files it created.  I see this
> reference as "Num-parts: #".  I could not find a method which
> I can call to check for the total numbers of files it will output.
> What would be helpful would be an array of files it created.

> Once I have the body handle I can then print it out only if it does
> not contain attachments.

>     $bodyh = $entity->bodyhandle;
>    $IO = $bodyh->open("r") || die "open body: $!";
>    while (defined ($_ = $IO->getline)) {
>       print $_;
>    }
>    $IO->close;

> This does not work when there are attachments.  Do you have problems
> with this?  I keep getting the message :
> Can't call method "open" without a package or object reference at mime.pl line
> 63, <STDIN> chunk 245.

This is where things really start to differ, the folowing 2 routines are
defined in the module

sub _DUMP {
   # INTERNAL SUBROUTINE DO NOT USE
   # recursively retrieve entity part file names and MIME types
   my $self = shift;
   my $ent  = shift || $self->{ENT};

   # Output the body:



   }
   else {                           # it's single part
      # get file name and the MIME type
      my ($name,$path,$suffix) = fileparse($ent->bodyhandle->path, '\..*');
      my $type = $ent->head->get('Content-Type');
      ($type,undef) = split ';', $type, 2;
      my $enc  = $ent->head->get('Content-Transfer-Encoding');
      $enc = $enc ? $enc : '7-bit';  # assume 7-bit for all 'message' parts
      $type =~ tr/\s//;   # where did the spaces come from? they are gone now.
      return $name.$suffix.'/'.$type.'/'.$enc;
   }

Quote:
}

I got the idea for how to do the above bit from the mimedump example script
that comes with MIME-Tools, or the bit that does the work anyway. (Thanks
Andreas and Eryq, afterall credit where credit is due).

sub dump_parts {
   my $self = shift;
   my $entity = shift || $self->{ENT};




Quote:
}

To hang it all together I do something like this:

   my $ent = new Attach Header => $msg_hdr,
                          Body => $msg_bdy,
                           Dir => $wkg_dir,
                     MTypePath => $$stp{mime};


which will give me an array of elements something like this:

        msg--1272-1.doc/text/plain/7bit

Which when lobbed through a conversion Module returns a parsed string
suitable for displaying in a web browser:



probably a lot of code that I have missed out, for brevity, and also
much that is not needed, but hey this is my first attempt and I am
learning a hell of a lot about Perl :-).

--
--
Ian J. Garlick

Children are unpredictable.  You never know what inconsistency they're
going to catch you in next.
                -- Franklin P. Jones



Sun, 21 Jan 2001 03:00:00 GMT  
 MIME-tools help
[Posted and mailed]


Quote:

> Can you give some pointers on how to encode messages with MIME attachments
> using MIME-Tools? By RTFM'ing, I discovered that decoding such messages is
> straightforward, but just about nothing is mentioned about encoding them.

> Thanks,
> Eugene

Encoding is easy just fire up a mime enabled mime client send an attachment.
However I fail to see what this has to do with Perl? Only joking :-)

[snipped]

I think the reason very little is mentioned is because it is so easy with
these tools. Having said that I got this from somewhere so it must be there.

$obj->{ENT}   = build MIME::Entity
         From       => $user,
         'X-Mailer' => "whatever",
         Path       => $path,
         Type       => $obj->_TFOR($path),
         Encoding   => '-SUGGEST';

You need the X-Mailer bit to stop MIME-Tools putting its version in there.


where _ATTACH does something like this

sub _ATTACH {
   # INTERNAL SUBROUTINE DO NOT USE
   # attache and encode files to message
   my $self  = shift;


      return undef;
   }


      my $path = $self->{DIR} ? $self->{DIR}.'/' : '';
      $path .= $file;


      return undef unless ( -e $path );

      $self->{ENT}->attach(
         Path       => $path,
         Type       => $self->_TFOR($path),
         Encoding   => '-SUGGEST');
   }
   else {

         $self->_ATTACH($_);
      }
   }

Quote:
}

call it all like this

my $ent = new Attach Sender => $sender,
                Dir => $wkg_dir,
                Message => $msg,
                MTypePath => $stp{mime},

I have missed some things out here, like if you don't want to include a
message part just files you need to init the ENT differently but that is
how i did it.  Basically as I said it is just a oop module around the parts
of the tools I want to use in the way I want to use them. This wont suite
everyone, or I would post the whole module for comment. Also from lurking
here the practice of posting masses of code tends to be discouraged :-)

Sorry this is a bit breif and sketchy but I am a bit busy as I am alpha
testing some of this and other code at the moment.

Anyway hope that is enough to start you off.
--
--
Ian J. Garlick

Get Revenge!  Live long enough to be a problem for your children!



Sun, 28 Jan 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. MIME-tools/Mail-tools incompatible?

2. Problem with MIME::Lite and MIME-tools

3. MIME-Tools - MIME::Entity - extra newline problem

4. MIME::Tools - Getting All MIME Header Info for a Message

5. MIME::Tools decoding of a MIME::Entity

6. problems with MIME::Lite and MIME-tools

7. Problem with MIME::Lite and MIME-tools

8. Help with MIME-tools

9. Need help with MIME-tools

10. Help Please: I need to install MIME::TOOLS

11. MIME-tools 4.116 uploaded to CPAN [vital bug fix]

12. MIME-tools 4.112 available on CPAN

 

 
Powered by phpBB® Forum Software