Solved

Excel File corrupted when attached in MIME::Lite

Posted on 2010-11-19
8
1,979 Views
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

0
Comment
Question by:davidcummings
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
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
 

Author Comment

by:davidcummings
Comment Utility
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
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
Add this to your code:

use File::Basename;

I needed it because I want the file name vs. the file path.
0
 

Author Comment

by:davidcummings
Comment Utility
Thanks. It runs now, but I'm getting the same problem I had before.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Accepted Solution

by:
jeromee earned 500 total points
Comment Utility
Do you write and close the file before you try sending it?
0
 

Author Comment

by:davidcummings
Comment Utility
that'll do the trick.

written, yes. closed, no.

thank you for your help.
0
 

Author Closing Comment

by:davidcummings
Comment Utility
Stupid error on my part.
0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
Don't beat yourself up... we have all done it once.

Glad I was able to help!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Microsoft has released various new features which are capable of handling various tasks. One of these tasks is ‘Migration from pop3 to Exchange Server’. Pop3 data stores various data along mailboxes like contacts, tasks, etc. So, it becomes the need…
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now