Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Excel File corrupted when attached in MIME::Lite

Posted on 2010-11-19
Medium Priority
Last Modified: 2013-12-17
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

Question by:davidcummings
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 10

Expert Comment

ID: 34174631
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!

Author Comment

ID: 34174671
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?
LVL 10

Expert Comment

ID: 34174721
Add this to your code:

use File::Basename;

I needed it because I want the file name vs. the file path.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 34174939
Thanks. It runs now, but I'm getting the same problem I had before.
LVL 10

Accepted Solution

jeromee earned 2000 total points
ID: 34174966
Do you write and close the file before you try sending it?

Author Comment

ID: 34175868
that'll do the trick.

written, yes. closed, no.

thank you for your help.

Author Closing Comment

ID: 34175872
Stupid error on my part.
LVL 10

Expert Comment

ID: 34176260
Don't beat yourself up... we have all done it once.

Glad I was able to help!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question