Attaching Multiple Files to Email

Discussion in 'PHP' started by webmaster_TSU, Dec 19, 2007.

  1. #1
    I have some code that I feel should be working properly to attach multiple files to an email. Unfortunetly, it's only attaching the first file...I'm not sure what's wrong...

    //My TEST attach multiple emails
    function test_send_mail($to, $thm, $html, $path)
    {
    	if ($path!='')
    	{	
    		//using multiple files, this is added
    		$path = explode(", ", $path);
    		$i = 0;
    		print "Size of explode: ".sizeof($path)."<br />";
    		while($i < sizeof($path)){
    			$fp[$i] = fopen($path[$i],"r"); 	
    			if (!$fp[$i]) 	
    			{ 	
    				print "Can't read - ".$path[$i]."<br />"; 	
    				exit(); 	
    			} else { 	
    				print "Read - ".$path[$i]."<br />"; 	
    			} 	
    			$file1[$i] = fread($fp[$i], filesize($path[$i])); 	
    			fclose($fp[$i]);
    			$i = $i + 1;
    		} 
    	}
    	$boundary = "--".md5(uniqid(time()));	
    	$headers = "From: ".$_POST['email']."\r\n";
    	$headers .= "MIME-Version: 1.0\n"; 	
    	$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; 	
    	$multipart = "--$boundary\n"; 	
    	$multipart .= "Content-Type: text/html; charset=UTF-8\n"; 	
    	$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n"; 	
    	$multipart .= "$html\n\n";
    	$message_part = "--$boundary\n"; 		 		 	
    	if ($path!='')
    	{ 			
    		//changed this part
    		$i = 0;
    		while($i < sizeof($file1)){
    			$message_part .= "Content-Type: application/octet-stream\n";	
    			$message_part .= "Content-Transfer-Encoding: base64\n";		
    			$message_part .= "Content-Disposition: attachment; filename = \"".$path[$i]."\"\n\n";
    			$message_part .= chunk_split(base64_encode($file1[$i]))."\n";
    			$message_part .= "--$boundary--\n";
    			$i = $i + 1;
    		}
    	}
    	//$multipart .= $message_part."--$boundary--\n";
    	$multipart .= $message_part; 
    	if(!mail($to, $thm, $multipart, $headers)) 
    	
    	{ 
    	
    		echo "Sorry, your mail not send"; 		
    		exit(); 	
    	} 
    	else 
    	print "<br /><br /S><center><b>Thank you for your quote request submission!</b></center>";
    	print "<br /><center>We will contact you shortly regarding the request.</center>";
    	unlink ($path);
    }
    PHP:

     
    webmaster_TSU, Dec 19, 2007 IP
  2. webmaster_TSU

    webmaster_TSU Peon

    Messages:
    449
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Does anyone know?
     
    webmaster_TSU, Dec 20, 2007 IP
  3. webmaster_TSU

    webmaster_TSU Peon

    Messages:
    449
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well I figured out that this works:

    //My TEST attach multiple emails
    function test_send_mail($to, $thm, $html, $path)
    {
    	$eol="\r\n";
      	$mime_boundary=md5(time());
    
    	if ($path!='')
    	{	
    		//using multiple files, this is added
    		$path = explode(", ", $path);
    		$i = 0;
    		print "Size of explode: ".sizeof($path)."<br />";
    		while($i < sizeof($path)){
    			//if it's 'nobody', file does not exist, so skip
    			if($path[$i] != 'nobody'){
    				$fp[$i] = fopen($path[$i],"r"); 	
    				if (!$fp[$i]) 	
    				{ 	
    					print "Can't read - '".$path[$i]."'<br />"; 	
    					exit(); 	
    				} else { 	
    					print "Read - '".$path[$i]."'<br />"; 	
    				} 	
    				$file1[$i] = fread($fp[$i], filesize($path[$i]));
    				fclose($fp[$i]);
    			} else {
    				print "Skipping - '".$path[$i]."'<br />";
    			}
    			$i = $i + 1;
    		} 
    	}
    	$boundary = "--".md5(uniqid(time()));	
    	$headers = "From: ".$_POST['email']."\r\n";
    	$headers .= "MIME-Version: 1.0\n"; 	
    	$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; 	
    	$multipart = "--$boundary\n"; 	
    	$multipart .= "Content-Type: text/html; charset=UTF-8\n"; 	
    	$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n"; 	
    	$multipart .= "$html\n\n";
    	$message_part = ""; 		 		 	
    	if ($path!='')
    	{ 			
    		//changed this part
    		$i = 0;	
    		while($i < sizeof($file1)){
    			//if it's 'nobody', file does not exist, so skip
    			if($path[$i] != 'nobody'){
    				$message_part .= "--$boundary\n";
    				$message_part .= "Content-Type: application/octet-stream\n";	
    				$message_part .= "Content-Transfer-Encoding: base64\n";		
    				$message_part .= "Content-Disposition: attachment; filename = \"".$path[$i]."\"\n\n";
    				$message_part .= chunk_split(base64_encode($file1[$i]))."\n";
    			}
    			$i = $i + 1;
    		}
    	}
    	$multipart .= $message_part."--$boundary--".$eol.$eol;; 
    	if(!mail($to, $thm, $multipart, $headers)) 
    	
    	{ 
    	
    		echo "Sorry, your mail not send"; 		
    		exit(); 	
    	} 
    	else 
    	print "<br /><br /S><center><b>Thank you for your quote request submission!</b></center>";
    	print "<br /><center>We will contact you shortly regarding the request.</center>";
    	
    	unlink ($path);
    }
    PHP:
    Because I know everyone was just dying to know...

    Now the only problem is the file needs to be deleted on the server. I think I have to change unlink ($path); to be inside a loop and unlink ($path[$i]);
     
    webmaster_TSU, Dec 20, 2007 IP