Excel File corrupted when attached in MIME::Lite

I have a Perl script that writes an excel file and is supposed to email it. The excel file is generated perfectly: if I merely copy it over to a machine with Excel, it opens fine. However, when I attach it to the email, it seems to get corrupted and what was once about a 300 KB file is now about 300 Bytes. I'm modeling my code after other Perl scripts I've written in the past that successfully attach and send excel files but it doesn't seem to be working here.
my $filename = "pos_" . $date . ".xls";
        my $excel_file = "/path/to/" . $filename;
        my $workbook = Spreadsheet::WriteExcel->new("$excel_file");

        $workbook = &createLimitSheet($workbook);

        $text = "see attached excel file.";

        my $msg = MIME::Lite->new(
                From => 'from@example.com',
                To => 'to@example.com',
                Subject => 'Excel File',
                Type => 'text/html',
                Data => "$text",
        );


        my $att = MIME::Lite->new(
                Path => $excel_file,
                Type => 'application/vnd.ms-excel',
                Encoding => 'base64',
                Disposition => 'attachment',
        );

        $att->attr( 'Content-Description' => $filename );
        $att->delete( 'Date' );
        $att->delete( 'X-Mailer' );
        $att->delete( 'MIME-Version' );

        $msg->attach( $att );

        $msg->send('smtp', 'ip.address.of.SMTPserver', Timout => 20);

Open in new window

davidcummingsAsked:
Who is Participating?
 
jeromeeConnect With a Mentor Commented:
Do you write and close the file before you try sending it?
0
 
jeromeeCommented:
Here's what I have used successfully

$email->attach(Type     => 'application/x-microsoft-excel',
                   Encoding => 'base64',
                   Path     => $file_path,
                   Filename => basename($file_path));

Good luck!
0
 
davidcummingsAuthor Commented:
Hi jeromee,

Thanks for your response.

I implemented your suggestion but I wasn't able to get the code to run. What is basename and do I need it?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jeromeeCommented:
Add this to your code:

use File::Basename;

I needed it because I want the file name vs. the file path.
0
 
davidcummingsAuthor Commented:
Thanks. It runs now, but I'm getting the same problem I had before.
0
 
davidcummingsAuthor Commented:
that'll do the trick.

written, yes. closed, no.

thank you for your help.
0
 
davidcummingsAuthor Commented:
Stupid error on my part.
0
 
jeromeeCommented:
Don't beat yourself up... we have all done it once.

Glad I was able to help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.