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

last week I posted someting about using arguments. in 1 of those reactions of that is here the code again... but now what is in the file too

again the code:

#!usr/bin/perl use 5.6.1; use strict; use warnings; use Mail::Internet; use MIME::Parser; my $mail = Mail::Internet->new($ARGV[0]); my $headers = $mail->head()->header_hashref(); my $maximum = 100000; my $mailnummer = int(rand($maximum)); my $from = $headers->{From}->[0]; my $to = $headers->{To}->[0]; my $cc = $headers->{CC}->[0]; if ($cc eq ""){ $cc = 0; } my $date_time = $headers->{Date}->[0]; my $subject = $headers->{Subject}->[0]; if ($subject eq ""){ $subject = 0; } my $parser = MIME::Parser->new(); $parser->output_under('/tmp'); my $entity = $parser->parse($ARGV[0]); my $file; if ( $entity->is_multipart ) { for my $part ( $entity->parts ) { my $head = $part->head; if ( my $filename = $head->recommended_filename ) { $file = $filename; } } }
and here what is inside perltst:
From from.someone@sent.com Wed Dec 3 09:36:43 2008 Return-Path: <from.someone@sent.com> Received: from [19.168.20.16] ([19.345.456.12]) by mail2.ibb.nl (9.22.41.45678901/3.15.10) with ESMTP id mB56a +hFCh20714; Wed, 3 Dec 2008 09:36:43 +0100 Message-ID: <34567890.12345678@sent.com> Date: Wed, 03 Dec 2008 09:36:55 +0100 From: someone <from.someone@sent.com> User-Agent: Thunderbird 2.0.0.18 (Windows/20081105) MIME-Version: 1.0 To: perl@recieve.nl CC: zdf@cc.nl Subject: Test voor Michiel 's Perl programma Content-Type: multipart/mixed; boundary="------------000002060003020904090008" This is a multi-part message in MIME format. --------------000002060003020904090008 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit hey, ben benieuwd of dit mailtje goed aankomt in het bestand. Groet, frits --------------000002060003020904090008 Content-Type: text/plain; name="Niet zelf een nummer bedenken!.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="Niet zelf een nummer bedenken!.txt" SW4gZfafajkGAGEQRda789UIOKAJkljfqj8q8gdG9ldm9lZ2VuGAOGJHAFfagkl4538daj +a a3QgaGllcnZvb3IgQWRkSXRlbSBvZiBkZgBzdGFuZGFhcmQgcHJvZ3JhbW1hJ3MgdmFuIE +lC Qiag6FGIJ891rgahuClJCQQ== --------------000002060003020904090008--
the command in linux must be: perl emailprogramma3.txt perltst. and then the program will do everything automatically. at this moment the program will only look at the word perltst itself. but I want that the code above this will run through the program

I know that @ARGV[0] must be changed 2 times in the code, but who can tell me in what?

Bass-Fighter

Replies are listed 'Best First'.
Re: loading files
by poolpi (Hermit) on Dec 22, 2008 at 10:10 UTC

    Mail::Internet->new( ARG, OPTIONS );
    This constructor is waiting for a file descriptor or a reference to an array
    From the doc :
    ARG is optional and may be either a file descriptor (reference to a GLOB) or a reference to an array.

    So ARGV[0] is your file to be opened (or slurped).

    Almost Same thing with $parser->parse( INSTREAM );
    From the doc :
    The INSTREAM can be given as an IO::File, a globref filehandle (like \*STDIN), or as any blessed object conforming to the IO:: interface (which minimally implements getline() and read()).


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      when I change Mail::Internet->new(ARGV[0]); into Mail::Internet->new( ARG, OPTIONS );
      and my $entity = $parser->parse(ARGV[0]); into my $entity = $parser->parse(INSTREAM);

      I got this:
      Bareword "ARG" not allowed while "strict subs" in use at emailprogramma3.txt line 13.
      Bareword "OPTIONS" not allowed while "strict subs" in use at emailprogramma3.txt line 13.
      Bareword "INSTREAM" not allowed while "strict subs" in use at emailprogramma3.txt line 36.
      Execution of emailprogramma3.txt aborted due to compilation errors.

      and now?

        Try

        my $mail_file = $ARGV[0]; open my $mail_fh, "<", $mail_file or die "Couldn't open '$mail_file': +$!";

        and then put $mail_fh where you now have $ARGV[0].

Re: loading files
by poolpi (Hermit) on Dec 22, 2008 at 13:27 UTC

    You may try something like this (tested) example

    #!/usr/bin/perl use strict; use warnings; use IO::File; use Mail::Internet; defined $ARGV[0] or die 'Usage: perl emailprogramma3.txt perltst\n'; my $file = $ARGV[0]; my $fh = new IO::File; # See File::Spec catpath method my $path = '/where/is/perltst/' . $file; $fh->open("< $file") or die "Can't open $file\n"; my $mail = Mail::Internet->new($fh); $mail->print_body; $fh->close; __END__ Output : ------ Content-Type: text/plain; name="Niet zelf een nummer bedenken!.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="Niet zelf een nummer bedenken!.txt" SW4gZfafajkGAGEQRda789UIOKAJkljfqj8q8gdG9ldm9lZ2VuGAOGJHAFfagkl4538daj +a a3QgaGllcnZvb3IgQWRkSXRlbSBvZiBkZgBzdGFuZGFhcmQgcHJvZ3JhbW1hJ3MgdmFuIE +lC Qiag6FGIJ891rgahuClJCQQ== --------------000002060003020904090008--


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      alright, if I read this it could possibly work. what I only see is this: my $path = '/where/is/perltst/' . $file;
      does this mean that I every time I got an e-mail must change the path? that will be a lot of work at my working place.

        You haven't exactly been clear on what you actually want to achieve. Maybe File::Find or just glob or readdir (with opendir) will help you? All of these will return you a list of files in (or below) a directory. Maybe, if you want to process all the files in a directory, that's what you want?

        You can help us help you better by explaining what you want to achieve.

Re: loading files
by Anonymous Monk on Dec 22, 2008 at 08:55 UTC
    @ARGV must be changed 2 times in the code, but who can tell me in what?
    Ask the person who wrote the code.
      lol, so I must ask myself, thats a funny one:P