|
|
Question : sendmail with attachment
|
|
hi !
i want to use sendmail to send multiple attachments. the file extensions may/may not be different. also the files are large - 200kb and higher. please help.
thanks, Anant
|
Answer : sendmail with attachment
|
|
anant99,
"..Hence there should be no upload feature. The script will should attach files which are already on the server..."
Try this & let me know how it goes.
=========================multi_email_attachements.html
Manesh's - MAIL-A-FILE DEMO PAGE
Manesh's MAIL-A-FILE Demo
Mail-A-File © 1998-2099
=========================multi_email_attachements.pl #!/usr/local/bin/perl
$|++;
use CGI; $query=new CGI;
print "Content-type: text/html\n\n";
$dir_to_store="/tmp"; $no_of_files=0; @list/public_html/files/2.gif','/public_html/files/1.gif'); $boundary="Message-Boundary-19990614"; $mailprog ="/usr/lib/sendmail";
foreach ($query->param){ ## Store the value in a variable. ## The name of the variable is the same as the HTML form element name ## Eg if the HTML element is called name_sender, the PERL ## variable will be called $name_sender. $$_=$query->param($_); }
$no_of_files=scalar(@list);
if (! $no_of_files){ ## No file selected for upload print "At least one attachement must be provided. \n"; exit; }else{ ## Get the email ready for delivery!! open(MAIL, "| $mailprog -t "); print MAIL "To: $name_recipient <$email_recipient>\n"; print MAIL "From: $name_sender <$email_sender>\n"; print MAIL "CC: $cc\n"; print MAIL "BCC: $bcc\n"; print MAIL "Subject: $subject\n"; print MAIL "MIME-Version: 1.0\n"; print MAIL "Content-type: Multipart/1;\n"; print MAIL "\tboundary=$boundary\n"; print MAIL "\n"; print MAIL "\n"; print MAIL "--$boundary\n";
foreach (@list){ $uploaded_file=$_;
$tmp_uploaded_file=$_; $tmp_uploaded_file=~ s/\\/\//g; @tmp_uploaded_file=split(/\//,$tmp_uploaded_file);
$filename = $tmp_uploaded_file[$#tmp_uploaded_file];
$content="";
$nl=$/;
undef $/; open(FILE,$uploaded_file) || warn $!; $content=; close(FILE); $/=$nl; ## Print the header for that attachment. print MAIL "Content-type: application/octet-stream; name=\"$filename\"; type=Unknown\n"; print MAIL "Content-transfer-encoding: BASE64\n"; print MAIL "Content-disposition: attachment\n"; print MAIL "\n";
$content= encode_base64($content); ## Use base64 for encoding the contents
## Attach the file!! print MAIL $content; print MAIL "\n"; print MAIL "--$boundary\n"; }
#print MAIL "\n"; print MAIL "--$boundary--\n";
close(MAIL); }
print "Email has been sent \n";
sub encode_base64 ($;$){ my $res = ""; my $eol = $_[1]; $eol = "\n" unless defined $eol; pos($_[0]) = 0; # ensure start at the beginning while ($_[0] =~ /(.{1,45})/gs) { $res .= substr(pack('u', $1), 1); chop($res); } $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs # fix padding at the end my $padding = (3 - length($_[0]) % 3) % 3; $res =~ s/.{$padding}$/'=' x $padding/e if $padding; # break encoded string into lines of no more than 76 characters each if (length $eol) { $res =~ s/(.{1,76})/$1$eol/g; } $res; }
|
|
|
|
|