Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Newbie : Extracting Attchments from E-Mail and storing in a directory

by kev (Initiate)
on Oct 25, 2002 at 08:51 UTC ( [id://207929]=perlquestion: print w/replies, xml ) Need Help??

kev has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am a newbie in Perl and Linux (Mandrake 9.0 to be exact) and have been set the challenge of writing a script to attach an image to an e-mail message, and then extract the image from message at the other end and store in the
/home/me/webtest/mail/attachments/ 
folder. simplemail.pl
#!/usr/bin/perl -w use MIME::Entity; ####################### # # Define the varibles for the test # ######################## $toAddress ='me@localhost'; $fromAddress ='me@localhost'; $Subject ="Some Random Subject"; $message = ["<B>This is a test message</B> that <i>witters</i> on abou +t some random stuff<br>\n", "Why? I dunno, i guess it just feels like doing so<br>\n", "Anyway it's doing no harm is it?<br>\n", "<a href=\"http://www.someurl.com\">Somewebsite</a>"]; $aFileName = "someimage.gif"; $aType = "image/gif"; $aEncoding = "-SUGGEST"; ############################### # # The Donkey Work Part..... # ################################ ## Create the top-level, and set up the mail headers: $top = MIME::Entity->build(Type => "text/html", From => $fromAddress, To => $toAddress, Subject => $Subject, Data => $message); ## for plaine text e-mail change Type to "text/plain", # Attach a Signature.... $top->attach(Path=>"signature.txt", Disposition => "inline", Encoding => "8bit"); # Attach the Image.... $top->attach(Path => $aFileName, Type => $aType, Encoding => $aEncoding, Disposition => "attachment"); # Send the message open (MAIL,"|/usr/sbin/sendmail -t -oi"); $top->print(\*MAIL); close MAIL;
This above code covers part one adaquetly and displays correctly in Evolution(Mandrake 9) and OE(Win98). Looking round the perlmonks site i found this strip.pl
use MIME::Parser; sub read_email { my $dir = "/home/me/webtest/mail/attachments"; my $url = "http://127.0.0.1/mail/attachments"; my $parser = new MIME::Parser; $parser->output_dir($dir); my $entity = $parser->read(\*STDIN) || die "couldn't parse MIME stre +am"; my $head = $entity->head; my $content = $head->as_string . "\n"; my @parts = $entity->parts; my $body = $entity->bodyhandle; $content .= $body->as_string if defined $body; my $part; for $part (@parts) { my $path = ($part->bodyhandle) ? $part->bodyhandle->path : undef; if ($path =~ /msg-\d+.*\.doc/) { open(IN, $path) || warn "Couldn't open $path\n"; local $/ = undef; $content .= <IN> . "\n"; close IN; unlink ($path) || warn "Couldn't unlink $path\n"; } else { my $file = $path; $file =~ s/$dir//o; $content .= "\n--\nSaved attachment: $url$file\n--\n"; } } return $content; }
which from what I could asertain should do the job. I guess it needs to be called from Konsole with the following
perl strip.pl
It run's okay with no error messages, however nothing happens, i am confused. I have managed to install procmail and metamail (after finally sussing out that by csh it meant tcsh for the dependancy) if they would make my life easier. I would only want .gif .png .jpg and .jpeg files extracted.

Replies are listed 'Best First'.
Re: Newbie : Extracting Attchments from E-Mail and storing in a directory
by dada (Chaplain) on Oct 25, 2002 at 08:54 UTC
    if this is your whole script, I think you need to add this line at the end:
    read_email();
    you defined a subroutine, but never actually called it. no surprise the script does nothing at all :-)

    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

      Thanks, something happens now, but not what is expected It starts and justs sits there with a rectangular blob on a line of its own. No attchements stripped, nor messages altered. BTW i've also added the line
      #!usr/bin/perl -w
      to the top of strip.pl and no warnings are generated.
        ok, we are one step further :-)

        what your script is doing is: waiting for something to come on input (STDIN). this means you're supposed to call your script like this:

        perl strip.pl < file.msg
        supposing that file.msg is a file that contains the mail you are going to process.

        cheers,
        Aldo

        King of Laziness, Wizard of Impatience, Lord of Hubris

Re: Newbie : Extracting Attchments from E-Mail and storing in a directory
by kev (Initiate) on Oct 25, 2002 at 08:51 UTC
    p.s. Thanks in advance for any help you may be able to provide me with.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found