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

Hi Monks,

In one of my applictaion, we create the "tar" files.
The tar file is created using "Archive::Tar" module.

These files will be sent as attachments to the email using "MIME::Lite" module.
We are receiving the mails and the attachments.
But, when we open the attachment an error is displayed as "Error reading header after processing 0 records".

The code which I am using to send the tar file is :
my ($msg, $to_list, $cc_list, $subject, $MailFrom, $email, $file); $MailFrom = xxx@abc.com; $email = "Test data you got"; $subject = "Test mail"; $to_list = "santhi@xx.com"; $cc_list = "santhi@xx.xom"; $file = "newfile.tar"; $msg = MIME::Lite->new( From => $MailFrom, To => $to_list, Cc => $cc_list, Subject => $subject, Type => 'TEXT', Encoding => '7bit', Data => $email ); ### Attach a part... the make the message a multipart automatically: $msg->attach(Type => 'application/x-tar', Encoding => 'base64', Path => "$file" ); $msg->send() ;

Please let me know how can I resolve this error.

Thanks a lot!!
Santhi
  • Comment on Problem in sending "tar" files as attachment using MIME::Lite module
  • Download Code

Replies are listed 'Best First'.
Re: Problem in sending "tar" files as attachment using MIME::Lite module
by imp (Priest) on Dec 20, 2006 at 06:21 UTC
    I tested this snippet locally without modification, other than substituting my email address and adding use strict/warnings/MIME::Lite.

    I succesfully tested sending a mail with a tar file created by both my native tar binary and Archive::Tar. Both mails were received, and untarring the file on the other end worked fine.

    The only failure I encountered was when I did not add any files to the newfile.tar. A file created with this:

    perl -MArchive::Tar -e '$t = Archive::Tar->new(); $t->write("newfile.t +ar")'
    Yielded the following when reading the file with tar -tf:
    tar: Cannot identify format. Searching... tar: End of archive volume 1 reached tar: Sorry, unable to determine archive format.
    These tests were performed on OpenBSD 3.5, perl 5.8.2, Archive::Tar 1.10, MIME::Lite 3.01. Mails were read with mutt and gmail.
      hi,

      I think problem is with creating the Tar file. I am using the code as below:
      my $tar = Archive::Tar->new(); my @filelist = ("txt.del", "txt1.del"); $tar->add_files(@filelist); $tar->write("newpart.tar", 2); my $error = $tar->error(); print "error is $error \n";
      By using the $tar->write("newpart.tar", 2);, I am getting the error as "Invalid archive directory".
      But to use the $tar->write("newpart.tar");, the tar file size is very large.

      So which compression value can be used such that the tar file size will be less and will open without errors.

      Thank you,
      Santhi
        You could try opening the output file first, using PerlIO::gzip so that it gets compressed on output, then pass the file handle to Archive::Tar->write() -- something like this:
        use PerlIO::gzip; use Archive::tar; # ... open( my $tarfh, ">:gzip", "my_tarball.tar.gz" ); # ... $tar->write( $tarfh );
        (I haven't tried that myself, but that's what I would try.)

        UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).

        That error is not coming from your code snippet, correct? Is it coming from winzip (or some other extraction program) when you try to extract it? I wonder if your extraction program is barfing because it doesn't know how to handle the decompression. If you're going to compress the archive, you should name it either newpart.tar.gz or newpart.tgz. Then your extraction program may be better able to handle it or at least you've let others know the archive needs to be unzipped first. And if your mailing compressed, I wouldn't use the application/tar mime type, application/x-gtar would probably work better.

        -derby