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

Hi Monks, I have written a perl program which creates an excel sheet. At the end of the script i email it as an attachment. I get the email but the attachments gets corrupted. I have closed the excel before emailing it. Can anyone tell me what am i doing wrong. I am using MIME::Lite, Net::SMTP::TLS for emailing and Spreadsheet::WriteExcel for writing the excel sheet. The code is very long to add here. Let me know which parts you need i can give them. Thank you Akhila.

Replies are listed 'Best First'.
Re: Emailing attachments.
by zentara (Cardinal) on Jul 06, 2012 at 15:27 UTC
    Hi, I just tested mailing an excel sheet thru Gmail, with MIME::Lite and it worked fine. The only real difference, from yours, is I set the Encoding for the Excel attachment. I also used Net::SMTP::SSL instead of Net::SMTP::TLS, but I don't think the problem lies there. If you want to see a copy of my script, let me know.
    $msg->attach( Type => 'application/vnd.ms-excel', Path => './zz.xls', Filename => 'college_orders.xls', Encoding => 'base64' );

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Hi It worked for very small excel sheets but failed for sizes 0.025 and above. Let me know your view. Also if you want to see code. update:0.025MB Thank you Akhila.
        My sample xls file was .8MB or over 800K. If you are failing with a size limit, I doubt it is Perl's fault. FWIW, I had a similar problem awhile back, where I couldn't make any attachments over 3 MB. It turned out it was some limitation of the wireless repeater I was using. Can you test with a direct connection to an ethernet cable?

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
      Hi I am not having any luck with emailing attachments. Can you send me a copy of your script. Thank you Akhila
        Here you go:
        #!/usr/bin/perl use warnings; use strict; use Net::SMTP::SSL; + use MIME::Lite; # Implements the same API as Net::SMTP, but uses IO::Socket::SSL for i +ts # network operations. Due to the nature of "Net::SMTP"'s "new" method, + it # is not overridden to make use of a default port for the SMTPS servic +e. # Perhaps future versions will be smart like that. Port 465 is usually + # what you want, and it's not a pain to specify that. # GMAIL's virus scanners have trouble with .zip and .exe attachments?? # and the smtp server rejects mails with those attachments #$server = 'your-smtp-server'; my $server = 'smtp.gmail.com'; my $to = 'who@freak.com'; my $from_name = 'me'; my $from_email = 'me@gmail.com'; my $subject = 'smtp-ssl-auth attach test'; my $user = 'me@gmail.com'; my $pass = 'my_password'; my $msg = MIME::Lite->new( From => $from_email, To => $to, Subject =>'test message', Type =>'TEXT', Data =>'This is a test, i repeat only a test', ); #$msg->attach(Type =>'application/octet-stream', #Encoding =>'base64', #Path =>'./zenbw_l.jpg', #); $msg->attach( Type => 'application/vnd.ms-excel', Path => './zz.xls', Filename => 'college_orders.xls', Encoding => 'base64' ); my $smtps = Net::SMTP::SSL->new($server, Port => 465, DEBUG => 1, ) or warn "$!\n"; # I hust lucked out and this worked defined ($smtps->auth($user, $pass)) or die "Can't authenticate: $!\n"; $smtps->mail($from_email); $smtps->to($to); $smtps->data(); $smtps->datasend( $msg->as_string() ); $smtps->dataend(); $smtps->quit(); print "done\n"; #You can alternatively just put everything in the argument to $smtp->d +ata(), #and forget about datasend() and dataend();

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Emailing attachments.
by marto (Cardinal) on Jul 06, 2012 at 14:01 UTC

    "The code is very long to add here. Let me know which parts you need i can give them."

    <sarcasm>We need that part that's broken.</sarcasm> On a serious note, create a cut down version of your script, providing code which replicates the problem.

Re: Emailing attachments.
by Anonymous Monk on Jul 06, 2012 at 14:00 UTC

    Can anyone tell me what am i doing wrong.

    You forgot to binmode on line 42

      I am not familiar with binmode. Is it something to do with encoding? Below is my code where i think the problem might be.
      my $msg = MIME::Lite->new( From => 'fromid', To => "toid", Subject =>'Web trial for today', Type => 'multipart/mixed', ); $msg->attach( Type => 'application/vnd.ms-excel', Path => '/root/Webtrial.xls', Id => 'Webtrial.xls', ); my $mailer = new Net::SMTP::TLS( 'smtp.gmail.com', Port => '587', User => 'username', Password => 'xxxxxxxxxxx' ); $mailer->mail('fromid'); $mailer->to('toid'); $mailer->data; $mailer->datasend($msg->as_string); $mailer->dataend; $mailer->quit;