Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Downloading Excel Spreadsheets from Web-based Mail

by c4onastick (Friar)
on Feb 02, 2007 at 05:04 UTC ( #597883=perlquestion: print w/replies, xml ) Need Help??
c4onastick has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'll be honest, this is my first post here, and I'm fairly new to perl (I come from a PHP background, I'm very excited to start perl!). I've finally found a suitable project to try my (perl) hand at. I'm teaching a lab section of Engineering Computing this semester, and as part of the class, the students are to email me their work (which, so far, consists of excel workbooks). After manually saving 30-some-odd of these today, I said, "There's gotta be a better way". Enter perl. I've been doing some reading on WWW::Mechanize and have successfully implemented it... so far. I'm trying to get it to find all the submitted excel files in my inbox (done), and download them (having trouble with this). Here's what I have so far:
#!C:\perl\bin\perl.exe use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $url = ''; $mech->get($url); # login $mech->submit_form( form_number => 0, fields => { imapuser => 'c4onastick', pass => 'my_password_here', } ); # Navigate back to the first page that contains the most recent messag +es $mech->follow_link(url_regex => qr/page=1/ ); # Find all links (messages) with Lab 2 in the subject my @links = $mech->find_all_links( text_regex => qr/lab\s*2/i ); my $content; foreach my $link (@links) { print $link->url."\n"; print $link->text."\n"; print "Following...\n"; $mech->follow_link( url => $link->url); $mech->follow_link( url_regex => qr/\.xls/i ); #link to the attach +ed excel file $content .= $mech->content; # this might be where it is breaking d +own last; # Jump out after one for testing $mech->back; } mkdir "Eng_files", 0755 or die "Couldn't create directory: $!"; chdir "Eng_files" or die "Can't change directory: $!"; open(FH, ">", "test.xls"); print "Writing out...\n"; print FH $content; print "Done.\n\n";
So far, it works, it writes out and everyone is happy, except excel. It wont let me open them (I get a funky ready-only error, I don't think its excel's fault, I think I'm doing something wrong in the perl above). I'll eventually save these with the student's name in the filename (which I can also get from the message), but for now, I need to be able to open them to grade them! Thanks in advance for your help. I really look forward to becoming part of this community! (Next project is a perl script to grade them for me!)

Replies are listed 'Best First'.
Re: Downloading Excel Spreadsheets from Web-based Mail
by Anonymous Monk on Feb 02, 2007 at 07:14 UTC
    Add binmode FH; or use $mech->save_content( $filename )
      Thank you both for your responses.
      I tried $mech->save_content('test.xls'); to no avail (perl complained that it couldn't find the method) but using binmode FH; did work. Thanks again!
Re: Downloading Excel Spreadsheets from Web-based Mail
by roboticus (Chancellor) on Feb 02, 2007 at 11:43 UTC

    Another possibility:

    It may be the umask setting for the webserver's process--it may be masking the write bit on the file when created. That might explain the read-only error.

    Also: This is a very good first post! ++


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://597883]
Approved by randyk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-06-19 03:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.