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

Hello Perlmonks, I'm having trouble sending an excel file as an attachment. The email is sent successfully and I receive the file, but when I open the file it is blank. I know that the excel file is valid because I have manually ftp'd it to my machine. Here is my subroutine:
sub SendEmail { my $file = shift; my $mailTo = shift; my $top = MIME::Entity->build(Type => "multipart/mixed", To => $mailTo, Subject => "test"); $top->attach(Path => $file, Type => 'application/vnd.ms-excel', Disposition => 'attachment') or die "Problem attachin +g $file: $!"; $top->smtpsend; }
Am I missing something? Thanks for your help.

Edit: Allow me to give a better description of the file that I receive. It comes to my email as an attachment and opens with Excel. The file itself has one sheet in the workbook, but no data.

If I manually ftp the file to my machine it opens with the data that I expect to be in it. The file is not that big...about 2 MB or so.

Replies are listed 'Best First'.
Re: Sending excel file as an attachment
by Jenda (Abbot) on Apr 12, 2011 at 19:26 UTC

    Any chance you forgot to close the file first? Happened a few times to various people ...

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      That was it! I closed the logfile and input file, but not the workbook.

      Thanks Jenda.

      That still begs the question "Why did the attach method not return an error"?

      I had a quick look at the source-code of this method. It calls the build method but that method does not check if the file exists or is accessible. So there is no error checking done here and the usual "or die ..." invocation gives a false sense of security.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        It would return an error if the file did not exist at all, but it can hardly know you have it opened somewhere and that there are some data waiting to be written in some buffers or something. Or even that some object holds data that it will write into that file once you tell it to or destroy the object.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Sending excel file as an attachment
by Anonymous Monk on Apr 12, 2011 at 19:38 UTC
    Try using Mail::SendEasy to send the file instead.
Re: Sending excel file as an attachment
by petecm99 (Pilgrim) on Apr 12, 2011 at 19:37 UTC
    Perhaps try a different attachment type - whenever I email Excel files, I use Type=>'image/text' and Encoding=>'base64'.