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

I am using the below script to email a ZIP file attachment, but when I receive the email and try to open the zip file I receive error "The archive is either in unknown format or damaged". What am I doing wrong when trying to email the ZIP attachment?? This script is being run on Windows 2000. Any help is appreciated.
#!c:\perl\bin\perl #-w # #use strict; use Time::Format qw(%time %strftime %manip); use Net::SMTP; use File::Copy; $ctime="$time{'hh:mm'}"; $yesterday="$time{'yyyymmdd', time-24*60*60}"; $lastmonth="$month_len[$month - 1]"; $cdate="$time{'yyyy.mm.dd'}"; $file1="OPTUS.ECBOT.ZIP"; $log="email.log"; my $subject = "subject"; my $mailfrom = "email@mail.com"; my $mailto = "email@mail.com"; print "Sending message to $mailto\n"; my $stat_filename = "email.log"; my ($serverName) = "mailserver"; my $smtp = Net::SMTP->new($serverName, Timeout => 60); # die "Couldn't connect to server $smtp" unless $smtp; $smtp->mail( $mailfrom ); $smtp->to( $mailto ); $smtp->data(); $smtp->datasend("From: ", $mailfrom, "\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"OPTUS.ECB +OT.ZIP\"\n"); $smtp->datasend("Content-Type: application/zip; name= $file1 "); $smtp->dataend(); $smtp->quit(); exit;

Replies are listed 'Best First'.
Re: Emailing ZIP Attachment
by tirwhan (Abbot) on Dec 23, 2005 at 22:00 UTC

    SMTP only understands 7-bit ASCII characters. Anything you want to send via email that's not in that character set needs to be encoded in such a way that it is. For binary attachments you should use Base64 encoding.

    I'd recommend using the modules from the Perl Email Project, they make the whole business of sending email relatively painless. In this case you should look at Email::MIME::Creator and Email::Send.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Emailing ZIP Attachment
by zentara (Cardinal) on Dec 23, 2005 at 22:57 UTC
    Try this if you are pressed for time:
    #!/usr/bin/perl use warnings; use strict; use MIME::Lite; $MIME::Lite::AUTO_CONTENT_TYPE = 1; #-- create the multipart container my $msg = MIME::Lite->new ( From => $query->param('fromwho'), To => $_, Subject => $query->param('subject'), Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; #-- add the text message part $msg->attach ( Type => 'TEXT', Data => $query->param('message'), ) or die "Error adding the text message part: $!\n"; #-- add the ZIP file $msg->attach ( Type => 'AUTO', Path => "../".$file, Filename => $file, Disposition => 'attachment' ) or die "Error adding $file: $!\n"; $msg->send;

    I'm not really a human, but I play one on earth. flash japh
Re: Emailing ZIP Attachment
by shiza (Hermit) on Dec 23, 2005 at 22:05 UTC
    I do not have any experience with the module(s) you're using. However, Mime::Lite has always treated me right. It might be of use to you.
Re: Emailing ZIP Attachment
by hsinclai (Deacon) on Dec 23, 2005 at 23:57 UTC
    I keep banging the Mail::SendEasy drum -- it makes this process so simple!
Re: Emailing ZIP Attachment
by reneeb (Chaplain) on Dec 24, 2005 at 01:58 UTC
    I would recommend to use Mail::Sender. It makes things soooo easy... ;-)
Re: Emailing ZIP Attachment
by ptum (Priest) on Dec 23, 2005 at 21:54 UTC

    You seem to have picked a bad time to post -- looks like most monks are off to enjoy Christmas. Knowing nothing about Net::SMTP, I'd wildly guess that your problem has something to do with needing to specify binary mode. Looking (briefly) at Net::SMTP::Multipart, I wonder if that would be more applicable to your problem?

    Hopefully someone actually knowledgable will wipe the eggnog from their chin and give you a real answer.

    Merry Christmas!


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Emailing ZIP Attachment
by Joost (Canon) on Dec 23, 2005 at 23:36 UTC

      HI Thanks for this information, after many try I finally have a Correct script using sendmail, this is my script you need some thinks

      1. $file are tha path to find the file 2. $namefile is the realname tu present un the e-mail 3. In $mail{body} you need know the TEXT after <<TEXT is the same of the final, and need start the line un the program, not indent. In my example user FINALBODY.

      This are my changes for work:

      use MIME::QuotedPrint; use MIME::Base64; use Mail::Sendmail; $host = "look.server.com.co"; $username = "MyEmailUser"; $password = "MyEmailPass"; $from = 'Willger@server.com.co'; $to = 'Torres@Server.com.co'; $mboundary="-FINALBLOCKgc0p4Jq0M2Yt08jU534c0p"; #this you cand select + any compouse of alfanumeric, but not change $mensaje = "Este es un mensaje en texto\n\n\n"; $html = "<HTLM><head><meta http-equiv=\"Content-Type\" content=\"text +/html; charset=ISO-8859-5\"></head> <p></p> <b>TEST TABLE</b> <p></p> <table border=1><tr><th>COLUMN1</th><th>COLUMN2</th><th>COLU +MN3</th></tr> <tr><th>DATA1</th><th>DATA2</th><th>DATA3</th></tr> <tr><th>NEXT1</th><th>NEXT2</th><th>NEXT3</th></tr> </table></HTML>"; %mail = ( 'To' => $to, 'From' => $from, 'Subject' => $asunto.$hora, 'Content-type' => 'multipart/mixed; charset=iso-8859-1; boundar +y='.$mboundary, ); ##Convert the file to encode base open (F, $mylocationfile) or die "Cannot read $file: $!"; binmode F; undef $/; $cfile = encode_base64(<F>); close F; $cfilelen = length $cfile; ##to start the next section of the mail $mboundary = '--'.$mboundary; $mail{body}= <<FINALBODY; $mboundary Content-Type: text/html; charset="iso-8859-1" $mensaje $html $mboundary Content-Disposition: attachment; filename="$mfilename" Content-Type: application/zip; name="$mfilename" Content-Transfer-Encoding: base64 Content-Length: $cfilelen $cfile $mboundary-- FINALBODY

      Thanks, And I hope help you.

      Willger Torres