LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to send zip file using Perl (https://www.linuxquestions.org/questions/programming-9/how-to-send-zip-file-using-perl-4175413936/)

oihsu21 06-28-2012 09:31 PM

How to send zip file using Perl
 
My server does not have MIME:Lite. Here is my code for sending out a zip file using Perl with attachment:
#!/usr/bin/perl

use File::stat;
use Time::localtime;
use Net::SMTP;

my $attachFile = 'pc.zip';
my $boundary = 'frontier';
my $data_file = '/home/myID/myPC.zip';

open(DATA, "$data_file") || die("Could not open the file");
my @zip = DATA;
close(DATA);

print "Sending email...\n";
$smtp = Net::SMTP->new("remotesmtp.company2.com",
Hello => 'remotesmtp.company2.com',
Timeout => 60);

$smtp->mail("abc\@company1.com");
$smtp->recipient("xyz\@company2.com");
$smtp->data();
$smtp->datasend("$Subj\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nSee attached zip file.\n\n\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Transfer-Encoding: base64 \n");
$smtp->datasend("Content-Type: application/zip; name=\"$attachFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachFile\"\n");
$smtp->datasend("\n");
$smtp->datasend("$data_file\n");
$smtp->datasend("--$boundary--\n");
$smtp->dataend();
$smtp->quit;

I can send the zip from the server end, but at the receiving end, the attachment cannot be opened due to invalid file format. What step did I do wrong? How should I prepare the zip file? Should the zip file be converted to base 64 text file first? I have tried either way. I got the same error.

Please help. Thanks!

kbp 06-28-2012 10:22 PM

You probably need to uuencode it.

oihsu21 06-28-2012 11:17 PM

Thanks
 
thank you! I did it either way, got the same error.

ramsforums 06-30-2012 09:38 PM

Quote:

Originally Posted by oihsu21 (Post 4714630)
My server does not have MIME:Lite. Here is my code for sending out a zip file using Perl with attachment:
#!/usr/bin/perl

use File::stat;
use Time::localtime;
use Net::SMTP;

my $attachFile = 'pc.zip';
my $boundary = 'frontier';
my $data_file = '/home/myID/myPC.zip';

open(DATA, "$data_file") || die("Could not open the file");
my @zip = DATA;
close(DATA);

print "Sending email...\n";
$smtp = Net::SMTP->new("remotesmtp.company2.com",
Hello => 'remotesmtp.company2.com',
Timeout => 60);

$smtp->mail("abc\@company1.com");
$smtp->recipient("xyz\@company2.com");
$smtp->data();
$smtp->datasend("$Subj\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nSee attached zip file.\n\n\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Transfer-Encoding: base64 \n");
$smtp->datasend("Content-Type: application/zip; name=\"$attachFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachFile\"\n");
$smtp->datasend("\n");
$smtp->datasend("$data_file\n");
$smtp->datasend("--$boundary--\n");
$smtp->dataend();
$smtp->quit;

I can send the zip from the server end, but at the receiving end, the attachment cannot be opened due to invalid file format. What step did I do wrong? How should I prepare the zip file? Should the zip file be converted to base 64 text file first? I have tried either way. I got the same error.

Please help. Thanks!

First can you send a Text file and see it works?

kbp 07-01-2012 06:36 AM

You could try opening the file in binary mode.

MisterBark 07-04-2012 10:24 PM

Well, first $data_file does not contain your file but its path!

Then, open should have "<" like this : open(DATA, "<$data_file")

Then, you MUST encode it in base64 :
use MIME::Base64; # beginning of your program
encode_base64(...) # to encode.

So to conclude :
Code:

use MIME::Base64; # beginning of your program

...

my $zipdata; # don't forget to declare it
open(DATA, "<$data_file") || die("Could not open the file ($!)");
read(DATA, $zipdata, (stat($data_file))[7] ); # this reads the whole file and puts its content into $zipdata
close(DATA);

...

$smtp->datasend("Content-Transfer-Encoding: base64\n"); # (remove the space)
$smtp->datasend("Content-Type: application/zip; name=\"$attachFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachFile\"\n");
$smtp->datasend("\n");
$smtp->datasend( encode_base64($zipdata)."\n" );
$smtp->datasend("--$boundary--\n");

PS: I hope your smtp module splits the lines!
Because in the smtp protocol, the lines are limited.
If you get a HUGE line of base64, you MUST cut it in pieces of 76 chars.

onebuck 07-05-2012 08:43 PM

Moderator Response
 
Moved to <programming>

CTM 07-23-2012 08:59 PM

Mail::Sender may interest you, particularly the MailFile method.


All times are GMT -5. The time now is 02:38 AM.