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! |
You probably need to uuencode it.
|
Thanks
thank you! I did it either way, got the same error.
|
Quote:
|
You could try opening the file in binary mode.
|
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 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. |
Moderator Response
Moved to <programming>
|
Mail::Sender may interest you, particularly the MailFile method.
|
All times are GMT -5. The time now is 02:38 AM. |