6
votes

I'm trying to run a script that takes a text file, compresses it using Archive::Zip, and sends the zip file as an attachment via smtp using Email::Sender to create a mime message.

I can send txt files in perl without corruption. I can send the file that perl zips manually without corruption. I can not send a manually zipped file through perl.

I suspect my problem is with either reading the zipped file, or creating the MIME message. Here is the relevant code, which is essentially the code from the synopsis of Email::MIME, where $fileToSend is the path to the zipped file.

Any ideas?

use strict;
use warnings;
use Email::MIME;
use Email::Sender::Transport::SMTP;
use Email::Sender::Simple qw(sendmail);
use Archive::Zip qw( :ERROR_CODES :CONSTANTS);
use IO::All;

my $message = 
      Email::MIME->create(
      header_str => [
    From    => $sender,
    To      => $recipient,
    Subject => $subject,
  ],
          attributes => {
              filename     => $filename,
              content_type => 'application/zip',
              disposition  => 'attachment',
              name         => $filename,
          },
          body => io($fileToSend)->binary->all,
          #body => io($fileToSend)->all,
      );
2
What's the difference between the sent and received files?Tim
The recieved zip file is slightly smaller than the sent zipped (822 kb vs 893 kb). That's why I suspect io() to be the problem.Dodd10x
why this complex method, just send file as attachment?netawater
@netawater - feel free to add your solution as an answerDodd10x

2 Answers

5
votes

Found the problem finally. Adding this line did the trick.

$message->encoding_set( 'base64' );
0
votes
my $transport = Email::Sender::Transport::SMTP->new({
                   host => $smtpserver,
                   port => $smtpport,
                   sasl_username => $smtpuser,
                   sasl_password => $smtppassword,
                   ssl => 'starttls'});

Email::Stuffer->from('[email protected]')
          ->to('[email protected]')
          ->text_body('hello')
          ->attach_file ('zipfile')
          ->transport($transport)
          ->send();