Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

filename of an attachment

by Bass-Fighter (Beadle)
on Dec 08, 2008 at 13:13 UTC ( [id://728933]=perlquestion: print w/replies, xml ) Need Help??

Bass-Fighter has asked for the wisdom of the Perl Monks concerning the following question:

I have made an program witch logs my headers from an e-mail. almost everything does what it must do, but I can't get the filename of an attachment logged. this is the piece of the script witch doesn't work:
#!usr/bin/perl + use v5.8.0; use strict; use warnings; use Mail::Internet; use Mail::Field; use MIME::Head; my $mail = Mail::Field ->new([ <STDIN> ]); my $header = $mail->head()->header_hashref(); my $field = 'Content-Type'; my ($Type, $subtype) = split('/', $field->mime_type); if ($Type eq 'multipart') { print $field->{'Content-Disposition'}->filename; }
If you know what I do wrong, pleace tell me

Replies are listed 'Best First'.
Re: filename of an attachment
by rminner (Chaplain) on Dec 08, 2008 at 13:54 UTC
    $Type contains the splitted result of field->mime_type. You are however splitting at the character '/' therefore it is impossible that $Type contains the pattern 'multipart/mixed'. If $field->mimetype returns 'multipart/mixed' then $Type will contain 'multipart' and $subtype will contain 'mixed'.
    Update:

    looking at the code i was also confused by the way you created your fields 'object'. You just assign the string 'Content-Type' to a variable called $field and rely on perl symbol table voodoo to dereference it to MIME::Field::ContType. I recommend that you have a look at the syntax used in the example of MIME::Field::ContType. It might help you getting closer to a solution.

    Another problem is: assuming the desired information is contained in the $header - you aren't accessing it anywhere.

    If you want to have a look at some working examples you might try searching google for o'reillys perl cookbook. It contains complete example scripts which might help you solve common problems.

      yes, you right, but now I have the problem that my program freezes somewhere between the stdin and the print, do you know the solution for that too?
      ok thx i will do that, I hope I can find something
Re: filename of an attachment
by webfiend (Vicar) on Dec 08, 2008 at 17:56 UTC

    First challenge: It is suggested in the documentation for Mail::Internet that you use MIME::Entity for multipart support. Mail::Internet and Mail::Field are explicitly not going to help you in this context. You already use MIME::Head, so I'm going to assume you have MIME::Tools installed.

    Second challenge: Test data would be really nice. But hey, I had to look up how to do it myself, so no points against you. I'm not even sure I'm doing it right myself. I generated a test email and attached it to the code in the __DATA__ chunk.

    use warnings; use strict; use MIME::Parser; my $parser = MIME::Parser->new(); # The message and attachment are extracted to disk, so there will be a # couple of files in this directory. $parser->output_under('/tmp'); my $entity = $parser->parse(\*DATA); if ( $entity->is_multipart ) { # This message is multipart, so look at each part for my $part ( $entity->parts ) { my $head = $part->head; # We only care about attached content in this example. if ( my $filename = $head->recommended_filename ) { print "Attachment name: $filename\n"; } } } __DATA__ Content-Type: multipart/mixed; boundary="----------=_1228756284-7253-0 +" Content-Transfer-Encoding: binary MIME-Version: 1.0 X-Mailer: MIME-tools 5.427 (Entity 5.427) From: me@myhost.com To: you@yourhost.com Subject: Hello, Nurse! This is a multi-part message in MIME format... ------------=_1228756284-7253-0 Content-Type: text/plain; name="short.txt" Content-Disposition: inline; filename="short.txt" Content-Transfer-Encoding: binary This is a short text file ------------=_1228756284-7253-0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: binary some literal text ------------=_1228756284-7253-0--

    Hopefully you can fill in the remaining blanks for your app, or somebody else can show an even more streamlined way to do it. Have fun!

      this is much better than it was, so thank you webfiend. it almost works already. I must only look at this:
      $parser->output_under('/tmp'); my $entity = $parser->parse(\*DATA);
      I want to do it with a standard in. this because the program will be set on a server where the mail will sent to in the form of a Bcc. and another thing: your testmail is fine;)
Re: filename of an attachment
by Anonymous Monk on Dec 08, 2008 at 13:24 UTC
    This is syntax error
    print $field->'Content-Disposition'->filename;
      jeah I know witch part is not good in the code, but I don't know in what way I can get it work
        $blah->{blah}->filename?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found