in reply to Re^2: Trouble emailing zip file
in thread Trouble emailing zip file

I was able to get the following to send the zip file but it's corrupted

Does anyone know why the zip file arrives corrupted but I can open the same zip file when using a shell script?

use strict; use warnings; use MIME::Base64; use IO::File; my ($subject, $mach, $from, $to, $attachment, $fh, $content); $subject = "Test Mail"; $from = 'user@email.com'; $to = 'user@email.com'; $attachment = "test.zip"; $fh = "mailbody.txt"; open(FILE, '>', $fh) or die "Cannot open $fh: $!"; print FILE "This is a test"; close(FILE); open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL "FROM: $from\n"; print MAIL "TO: $to\n"; print MAIL "Subject: $subject\n"; print MAIL "Content-Type: multipart/mixed; boundary=frontier\n"; print MAIL "--frontier\n"; print MAIL "Content-Type: text/plain; charset=us-ascii\n\n"; open(FILE, '<', $fh) or die "Cannot open $fh: $!"; print MAIL <FILE>; close(FILE); print MAIL "\n\n"; print MAIL "--frontier\n"; chomp(my $basename=`basename $attachment`); print MAIL "Content-Disposition: attachment; filename=$basename\n"; print MAIL "Content-Type: application/octet-stream; name=$attachment\n +\n"; print MAIL "encode_base64( read_file($attachment) )"; open(FILE, '<', $attachment) or die "Cannot open $attachment: $!"; print MAIL <FILE>; print MAIL "\n"; close(FILE); close(MAIL);

Replies are listed 'Best First'.
Re^4: Trouble emailing zip file
by Corion (Patriarch) on Jul 05, 2018 at 19:08 UTC
    open(FILE, '<', $attachment) or die "Cannot open $attachment: $!";

    You most likely want binmode:

    open(FILE, '<', $attachment) or die "Cannot open $attachment: $!"; binmode FILE; ...

    Also, you want to eliminate the use of `basename...` in favour of File::Basename::basename:

    use File::Basename 'basename';

    Also, please consider using a module to send mail, like MIME::Lite instead of talking to sendmail directly...

      Thanks Corion,

      Do you know if MIME::Lite is a pure perl module this way I can copy the source code and use it in my script?

        Yes, MIME::Lite can directly be pasted into your script, or alternatively, simply downloaded into a file Lite.pm. You can easily see whether a module is pure Perl by looking at whether its distribution package contains .xs or .xsp files.

Re^4: Trouble emailing zip file
by poj (Abbot) on Jul 05, 2018 at 19:53 UTC

    Why the second print statement ?

    print MAIL "encode_base64( read_file($attachment) )"; open(FILE, '<', $attachment) or die "Cannot open $attachment: $!"; print MAIL <FILE>;

    Try replacing those 3 lines with

    open(FILE, '<', $attachment) or die "Cannot open $attachment: $!"; binmode FILE; while (read(FILE, my $buf, 60*57)) { print MAIL encode_base64($buf); }
    poj
      Thanks poj but the attachment is still arriving corrupted.

        Try

        #!/usr/bin/perl use strict; use warnings; use MIME::Base64; use IO::File; use File::Basename 'basename'; my $from = ''; my $to = ''; my $subject = "Test Mail"; my $mailbody = "This is a test"; my $attachment = "test.zip"; my $basename = basename($attachment); open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL << "EOF"; From: $from To: $to Subject: $subject Content-Type: multipart/mixed; boundary=frontier --frontier Content-Type: text/plain; charset=us-ascii $mailbody --frontier Content-Disposition: attachment; filename=$basename Content-Transfer-Encoding: base64 Content-Type: application/octet-stream; name=$attachment EOF open FILE, '<', $attachment or die "Cannot open $attachment: $!"; binmode FILE; while (read(FILE, my $buf, 60*57)) { print MAIL encode_base64($buf); } close FILE; print MAIL "\n--frontier\n"; close MAIL;
        poj