LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2012, 09:31 PM   #1
oihsu21
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Rep: Reputation: Disabled
Unhappy 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!

Last edited by oihsu21; 06-28-2012 at 09:32 PM.
 
Old 06-28-2012, 10:22 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
You probably need to uuencode it.
 
Old 06-28-2012, 11:17 PM   #3
oihsu21
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks

thank you! I did it either way, got the same error.
 
Old 06-30-2012, 09:38 PM   #4
ramsforums
Member
 
Registered: Jun 2007
Posts: 66

Rep: Reputation: 0
Quote:
Originally Posted by oihsu21 View Post
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?
 
Old 07-01-2012, 06:36 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
You could try opening the file in binary mode.
 
Old 07-04-2012, 10:24 PM   #6
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 104

Rep: Reputation: 6
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.

Last edited by MisterBark; 07-04-2012 at 10:31 PM.
 
Old 07-05-2012, 08:43 PM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved to <programming>
 
Old 07-23-2012, 08:59 PM   #8
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware
Posts: 308

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


Reply

Tags
attachment, perl, smtp



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to zip a variable with PERL without reading or writing a file? maxmeier12 Programming 4 06-04-2013 04:50 AM
How to find out a file in a WAR which in a zip without unzip the zip-file? thomas2004ch Linux - Software 3 09-30-2011 03:06 PM
how to send and receive files(txt,zip,pdf,jpe format) b/w two pc using RS-232 amit_pansuria Programming 10 05-28-2007 09:42 AM
create a self-extracting zip file with zip on solaris? samsolaris Solaris / OpenSolaris 3 10-15-2004 01:50 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration