LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 10: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 10:32 PM.
 
Old 06-28-2012, 11:22 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
You probably need to uuencode it.
 
Old 06-29-2012, 12:17 AM   #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, 10:38 PM   #4
ramsforums
LQ Newbie
 
Registered: Jun 2007
Posts: 23

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, 07:36 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

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

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 11:31 PM.
 
Old 07-05-2012, 09:43 PM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,442
Blog Entries: 4

Rep: Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503
Moderator Response

Moved to <programming>
 
Old 07-23-2012, 09:59 PM   #8
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware64 14.0 / 14.1
Posts: 131

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


Reply

Tags
attachment, perl, smtp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05: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 04: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 10:42 AM
create a self-extracting zip file with zip on solaris? samsolaris Solaris / OpenSolaris 3 10-15-2004 02:50 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 01:53 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration