Current location: Hot Scripts Forums » Programming Languages » Perl » file attachment problem on email script


file attachment problem on email script

Reply
  #1 (permalink)  
Old 01-10-07, 04:41 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
file attachment problem on email script

Hi to all!

i have a free CGI email script, which can send attachment along with mail to a list of people. when i am sent file name with character its send attachment to all mails.

but the problem is when i am trying to give filename with numeric value and space like that 'colour of money 10jan07.pdf' its creating problem, its not talking full name and for result attachment is nothing. attachment is going with full file name and file, but from the second sender of email list its only taking a part of the file name and nothing as an attachment.

I think there will be with file name taking by the script but can’t understand what to do with it.

Can any one help me with this?

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 01-10-07, 08:04 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sujata_ghosh
but the problem is when i am trying to give filename with numeric value and space like that 'colour of money 10jan07.pdf' its creating problem, its not talking full name and for result attachment is nothing.
Hi sujata,

Can I remind you (again) to always include a snippet of your code when asking for help? It helps others help you. First off, you should have some sort of sub routine making sure that files that are in the directory for attachments have no space in them. If I were you, I would include the following snippet before sending email:

PHP Code:

# NOTE: You will need to modify this routine if you have a large number of 

# files in your attachment directory as it will slow down the sending of email.
# As long as you have less than a few hundred files in that folder, you are ok
# When you surpass that number of files, just modify this code so that only 
# filenames that were recently added are verified/modded.

use File::Find;
$media_dir '/PATH TO FOLDER OF ATTACHMENTS/media'# NO TRAILING SLASH!!!

sub rm();
@
directories = ("$media_dir");
find(\&rm, @directories);

sub rm() {
my ($filename) = $_;

if (
$filename =~ /\.(pdf|txt|html|jpg|gif)$/i) {
   
$who $File::Find::name;
   
   
$filename =~ tr/a-z_A-Z0-9._//cd;
   
rename ("$who""$File::Find::dir/$filename") || die "$!\n";
   
}

Reply With Quote
  #3 (permalink)  
Old 01-11-07, 01:17 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
I am so sorry!

here is the code of send mail, which i am using.

Code:
####################### Send Mail ##################################
sub send_mail{
	
	my @boundaryv = (0..9, 'A'..'F');
	$PREF{'FOOTER'} =~ s/\\n/\n/g;
	$| = 1;
	srand(time ^ $$);
	my @boundaryv = (0..9, 'A'..'F');
	for (my $i = 0; $i++ < 24;)
	{
		$boundary .= $boundaryv[rand(@boundaryv)];
	}
	
    		#foreach my $file (@files)
     		#{
        		do { push @problem, "$file"; next; } if !-r "$upload_dir/$file"; # checks the existence and readability
        		my ($boundary, $ext) = (undef, undef);

        		#--------------------------------------------------- Obtain content-type of file
			($ext) = $file =~ m,\.([^\.]*)$,;
	 		$ext =~ tr/a-z_A-Z0-9._//cd;
         		my $fext = $mime{$ext};

        		#--------------------------------------------------- Generate multipart boundary
  			srand(time ^ $$);

  			for (my $i = 0; $i++ < 24;)
  	 		{
  	       			$boundary .= $boundaryv[rand(@boundaryv)];
  	 		}

        		#--------------------------------------------------- Send attatchments etc...
		
my $lines = '/home/httpd/vhosts/k****ij.com/httpdocs/rupeesp/rupeesplist.dat';
				open(EMAIL, $lines);
				my @lines = <EMAIL>;
				close(EMAIL);
				$mailprog = '/usr/sbin/sendmail';

				foreach my $line (@lines) 
				
		{
			($em, $ser1, $ser2) = split(/\:/, $line);

		if (($client1 eq "Essar") && ($ser1 eq "E"))
				{
        		open MAIL, "| $mail_prog -t" || die "I was not able to open the mail program<BR>contact the server's 					technical administrator.<BR><BR>If this is not admin, please let him/her<BR> know about this problem.";
				print MAIL "To: $em \n";
		    	print MAIL "From: Vikram Murarka <vikram\@k****ij.com>\n";
				print MAIL "Reply-to: Vikram Murarka <vikram\@k****ij.com>\n";
			
				print MAIL "Return-Path: Nilanjana <nilanjana\@k****ij.com>\n";
				print MAIL "X-Mailer: Custom-CGI: Web Mailer v1.02\n";
           		print MAIL "MIME-Version: 1.0\n";
	   			print MAIL "Subject: $INPUT{'subject'}\n";
				print MAIL "Content-Type: multipart/mixed; boundary=\"------------$boundary\"\n";
				print MAIL "\n";
				print MAIL "This is a multi-part message in MIME format.\n";

				print MAIL "--------------$boundary\n";
				print MAIL "Content-Type: text/plain; charset=us-ascii\n";
				print MAIL "Content-Transfer-Encoding: 7bit\n\n";
 
		   		print MAIL "Dear $ser2, \n\n";	

				print MAIL "$messtext \n\n";
				print MAIL "$distext \n\n";
				
			

	open(ATTACH, "com_attach.txt");
	@all = <ATTACH>;
	close(ATTACH);
	
	
			
	for ($i = 0; $i<=$#all; $i++) {
		$| = 1;
		$all[$i] =~ s/\n//g;
		#$all[$i] =~ s/ //g;
		$file = $all[$i];
		#do { print "Can't find ($upload_dir/$file)"; next; } if !-r "$upload_dir/$file"; # checks the existence and 			readability
		
		($ext) = $file =~ m,\.([^\.]*)$,;
	 	$ext =~ tr,a-z,A-Z,;
        my $fext = $mime{$ext};
		if ($fext && $fext =~ /^text/) {
			print MAIL "--------------$boundary\n";
			print MAIL "Content-Type: $fext; charset=us-ascii; name=\"$file\"\n";
			print MAIL "Content-Transfer-Encoding: 7bit\n";
			print MAIL "Content-Disposition: attachment; filename=\"$file\"\n\n";
			#print MAIL "$file";
	
			open INPUT, "$upload_dir/$file"; # should be readable, we checked above [-r]
			print MAIL while (<INPUT>);
			close INPUT;

		} else {
		
			print MAIL "--------------$boundary\n";
			print MAIL "Content-Type: $fext; name=\"$file\"\n";
			print MAIL "Content-Transfer-Encoding: base64\n";
			print MAIL "Content-Disposition: attachment; filename=\"$file\"\n\n";
			
			my $buf; $/=0;
			open INPUT, "$upload_dir/$file"; # should be readable, we checked above [-r]
			binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');
			while(read(INPUT, $buf, 60*57))
			{
			print MAIL &encode_base64($buf);
			}
			close INPUT;
         }    
		}

			print MAIL "\n--------------$boundary--\n";
           	print MAIL "\n";
			close MAIL;
		}


		if (($client2 eq "Renuka") && ($ser1 eq "R"))
				{

	open MAIL, "| $mail_prog -t" || die "I was not able to open the mail program<BR>contact the server's technical administrator.<BR><BR>If this is not admin, please let him/her<BR> know about this problem.";
				print MAIL "To: $em \n";
		    	print MAIL "From: Vikram Murarka <vikram\@k****ij.com>\n";
				print MAIL "Reply-to: Vikram Murarka <vikram\@k****ij.com>\n";
			
				print MAIL "Return-Path: Nilanjana <nilanjana\@k****ij.com>\n";
				print MAIL "X-Mailer: Custom-CGI: Web Mailer v1.02\n";
           		print MAIL "MIME-Version: 1.0\n";
	   			print MAIL "Subject: $INPUT{'subject'}\n";
print MAIL "Content-Type: multipart/mixed; boundary=\"------------$boundary\"\n";
print MAIL "\n";
print MAIL "This is a multi-part message in MIME format.\n";

print MAIL "--------------$boundary\n";
print MAIL "Content-Type: text/plain; charset=us-ascii\n";
print MAIL "Content-Transfer-Encoding: 7bit\n\n";
           			
		   		print MAIL "Dear $ser2, \n\n";

				print MAIL "$messtext\n\n";
				print MAIL "$distext\n\n";
				

	open(ATTACH, "com_attach.txt");
	@all = <ATTACH>;
	close(ATTACH);
	
		
	for ($i = 0; $i<=$#all; $i++) {
		$| = 1;
		$all[$i] =~ s/\n//g;
		#$all[$i] =~ s/ //g;
		$file = $all[$i];
		#do { print "Can't find ($upload_dir/$file)"; next; } if !-r "$upload_dir/$file"; # checks the existence and 			readability
		
		($ext) = $file =~ m,\.([^\.]*)$,;
	 	$ext =~ tr,a-z,A-Z,;
        my $fext = $mime{$ext};
		if ($fext && $fext =~ /^text/) {
			print MAIL "--------------$boundary\n";
			print MAIL "Content-Type: $fext; charset=us-ascii; name=\"$file\"\n";
			print MAIL "Content-Transfer-Encoding: 7bit\n";
			print MAIL "Content-Disposition: attachment; filename=\"$file\"\n\n";
			#print MAIL "$file";
	
			open INPUT, "$upload_dir/$file"; # should be readable, we checked above [-r]
			print MAIL while (<INPUT>);
			close INPUT;

		} else {
		
			print MAIL "--------------$boundary\n";
			print MAIL "Content-Type: $fext; name=\"$file\"\n";
			print MAIL "Content-Transfer-Encoding: base64\n";
			print MAIL "Content-Disposition: attachment; filename=\"$file\"\n\n";
			
			my $buf; $/=0;
			open INPUT, "$upload_dir/$file"; # should be readable, we checked above [-r]
			binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');
			while(read(INPUT, $buf, 60*57))
			{
			print MAIL &encode_base64($buf);
			}
			close INPUT;
         }    
	}

			print MAIL "\n--------------$boundary--\n";
           	print MAIL "\n";
			close MAIL;

		}
		

}


#
# $---------------------------------------------------------------------
##  S  U  B  -  E  N  C  O  D  E    B  A  S  E  6  4
## $----------------------------------------------------------------------#

 	sub encode_base64 #($)
  	{
    		my ($res, $eol, $padding) = ("", "\n", undef);

    		while (($_[0] =~ /(.{1,45})/gs))
     		{
	 		$res .= substr(pack('u', $1), 1);
	 		chop $res;
     		}

    		$res =~ tr#` -_#AA-Za-z0-9+/#;               		# ` help emacs
    		$padding = (3 - length($_[0]) % 3) % 3;   		# fix padding at the end

    		$res =~ s#.{$padding}$#'=' x $padding#e if $padding;    # pad eoedv data with ='s
    		$res =~ s#(.{1,76})#$1$eol#g if (length $eol);          # lines of at least 76 characters

    		return $res;
  	}
CODE FOR COMMON FILE

Code:
#### These are some settings!

# this is where the .cgi scripts are located (This is a PATH, not a URL)
# No trailing slash
$base_dir = "/home/httpd/vhosts/k****ij.com/cgi-bin/rupeesp";


# this is the temp dir, wher uploaded files are stored, it should be outside
# the web server root (ie. not www accessible) and should be CHMODed 777
# No trailing slash
$upload_dir = "/home/httpd/vhosts/k****ij.com/cgi-bin/temp";

# set this to "yes" or "no"
$check_type = "no";

# only if above is "yes" do you need to set the following var.
# allowed upload types. thes are the only file types allowed to be uploaded.
@allowed = (".gif",".jpg",".txt",".html",".htm",".xls",".dat",".doc",".pdf");

# Number of uploads allowed!!
$num_uploads = 10;

# Where is sendmail on your server
$mail_prog = "/usr/sbin/sendmail";

# url of where compose.cgi is located
$mail_url = "http://k****ij.com/cgi-bin/rupeesp/rupeemail1.cgi";

# this is the max upload size (in Kb), 1000Kb = 1 Mega byte.
$max_upload = "1000"; #in Kb
CODE FOR ATTACHEMENT A FILE

Code:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

	if ($ENV{'HTTP_REFERER'} !~ s/$ENV{'SERVER_NAME'}//g) {
		exit;
	}

require "rs_common.cgi";

################################################
################################################
## DO NOT EDIT OR COPY BELOW THIS LINE        ##
################################################
################################################

use CGI; 
$onnum = 1;

while ($onnum != 11) {
my $req = new CGI; 
my $file = $req->param("FILE$onnum"); 
	if ($file ne "") {
		my $fileName = $file; 
		$fileName =~ s!^.*(\\|\/)!!; 
		$newmain = $fileName;
		$newmain1 = $fileName;
		
		if ($check_type eq "yes") {
			for ($i = 0; $i<=$#allowed; $i++) {
				if (lc(substr($newmain,length($newmain) - 4,4)) eq $allowed[$i]){				
					$found = "1";
				}
				if (lc(substr($newmain1,length($newmain1) - 5,5)) eq $allowed[$i]) {
					$found = 1;
				}
			}
		} else {
			$found = 1;
		}
		$max_upload1 = $max_upload * 1000;
		if ($ENV{'CONTENT_LENGTH'} > $max_upload1){
			unlink ("$basedir/$fileName");
			print "Content-type: text/html\n\n";
			print "<h1>Error</h1>
			But the file you tried to upload is too big, the biggest file size allowed
			to be uploaded is, <b>$max_upload Kb</b>. ($ENV{'CONTENT_LENGTH'} gt $max_upload1)\n";
			exit;
		}
		if ($found ne "1") { 
		
			unlink ("$basedir/$fileName");
			print "Content-type: text/html\n\n";
			print "<h1>Error</h1>
			But you cannot upload that file type.\n";
			exit;
			
		} else {
			open (OUTFILE, ">$upload_dir/$fileName") || die "Couldn't copy file to server, make sure directory is Chmoded 777."; 
			while (my $bytesread = read($file, my $buffer, 1024)) { 
				print OUTFILE $buffer; 
			} 
			close (OUTFILE); 
			open(DATAFILE,">>com_attach.txt") || die "Couldn't write to attach.txt, make sure it exists and is CHMODed 666."; 
			print DATAFILE "$fileName\n";
			close(DATAFILE);
		}

	}
$onnum++;
}

print "Content-type: text/plain\n";
print "Location: $mail_url\n\n";

I am using a list of email for certain client. from that list the first client getting the email with correct attachment, but from 2nd onward no one getting the correct attachment name, and as a result no correct attachment.

as for example:

on client1 ie. "Essar" have 5 person in email list all of them will get same email with same attachment but the first person of ESSAR from email list is getting proper attached file with proper file name, rest are getting attachment but wrong file name and with 0 size.

if we would like to send a file called 'C O M 11Jan07.doc'
first person get this full name with correct file attached with email, but rest of 4 person get a file attached with the mail '7.doc' with 0 size.

hope now u will able to find out the problem.

sorry for the previous inconvenience.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP to email attachment problem arandlett PHP 6 01-22-09 01:12 PM
Write to php file with php script or create new file, it is possible? Oskare100 PHP 10 08-14-08 09:03 AM
Programmer Needed for File hosting script! Fabolous Job Offers & Assistance 3 06-15-06 10:56 PM
Creating Website Script Problem Dainbramaged05 PHP 4 06-30-04 12:29 PM


All times are GMT -5. The time now is 04:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.