Solved

Excel File corrupted when attached in MIME::Lite

Posted on 2010-11-19
8
2,034 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
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!
0
 

Author Comment

by:davidcummings
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?
0
 
LVL 10

Expert Comment

by:jeromee
ID: 34174721
Add this to your code:

use File::Basename;

I needed it because I want the file name vs. the file path.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

Accepted Solution

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

Author Comment

by:davidcummings
ID: 34175868
that'll do the trick.

written, yes. closed, no.

thank you for your help.
0
 

Author Closing Comment

by:davidcummings
ID: 34175872
Stupid error on my part.
0
 
LVL 10

Expert Comment

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

Glad I was able to help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In this video we show how to create an Address List 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 Organization >> Ad…

840 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