problems to use php mail() to attach more than 1 attachment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perhapscwk
    New Member
    • Sep 2007
    • 123

    problems to use php mail() to attach more than 1 attachment

    I use below to attach 2 attachment however only the once I check the email,
    I can only receive the first atatchment, and the second atatchment are always
    name as ATTXxX with 0k only....how come???
    ATT00001 (0.0 KB), firstfile.doc (96.6 KB)

    thanks.


    Code:
    	$num = md5(time()); 
    	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
    	
    	//Normal headers 
    	   $headers  = "From: AAA<ad@aaa.com>\r\n"; 
           $headers  .= "MIME-Version: 1.0\r\n"; 
           $headers  .= "Content-Type: multipart/mixed; "; 
           $headers  .= "boundary=".$num."\r\n"; 
           $headers  .= "--$num\r\n"; 
    
        // This two steps to help avoid spam    
        $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
        $headers .= "X-Mailer: PHP v".phpversion()."\r\n";    
    	
    	// With message 
           $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
           $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
           $headers .= "".$message."\n"; 
           $headers .= "--".$num."\n";  
    	
    	/* firstFile */ 
    	$fileatt = $_FILES['emailsentmsg']['name']; // Path to the file                  
    	$fileatt_type = $_FILES['emailsentmsg']['type']; 
    	$fileatt_temp = $_FILES['emailsentmsg']['tmp_name']; 
    	$fileatt_size = $_FILES['emailsentmsg']['size']; 
    	$fileatt_name = $fileatt; // Filename that will be used for the file as the attachment 
    	
    	$fp = fopen($fileatt_temp,'rb'); 
    	$file = fread($fp, $fileatt_size); 
    	$file = chunk_split(base64_encode($file)); 
    
        // Attachment headers 
    	   $headers .= "--{$mime_boundary}\n" . 
    	   $headers  .= "Content-Type:".$fileatt_type." "; 
           $headers  .= "name=\"".$fileatt_name."\"r\n"; 
           $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
           $headers  .= "Content-Disposition: attachment; "; 
           $headers  .= "filename=\"".$fileatt_name."\"\r\n\n"; 
           $headers  .= "".$file."\r\n"; 
           $headers  .= "--{$mime_boundary}\n"; 
    	   
    	fclose($fp);
    	unset($file);
    	unset($fileatt); 
    	unset($fileatt_type); 
    	unset($fileatt_name);
    
    	/* second File */ 
    	if ($emailsentmsg0){
    	$fileatt = $_FILES['emailsentmsg0']['name']; // Path to the file                  
    	$fileatt_type = $_FILES['emailsentmsg0']['type']; 
    	$fileatt_temp = $_FILES['emailsentmsg0']['tmp_name']; 
    	$fileatt_size = $_FILES['emailsentmsg0']['size']; 
    	$fileatt_name = $fileatt; // Filename that will be used for the file as the attachment 
    	
    	$fp = fopen($fileatt_temp,'rb'); 
    	$file = fread($fp, $fileatt_size); 
    	$file = chunk_split(base64_encode($file)); 
    
        // Attachment headers 
    	//$headers .= "--{$mime_boundary}\n" . 
        $headers  .= "Content-Type:".$fileatt_type." "; 
           $headers  .= "name=\"".$fileatt_name."\"r\n"; 
           $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
           $headers  .= "Content-Disposition: attachment; "; 
           $headers  .= "filename=\"".$fileatt_name."\"\r\n\n"; 
           $headers  .= "".$file."\r\n"; 
           $headers  .= "--{$mime_boundary}\n"; 
    	   
    		fclose($fp);
    		unset($file);
    		unset($fileatt); 
    		unset($fileatt_type); 
    		unset($fileatt_name);
    	}
    
    	$oksent = mail($emailto, $subject, $message, $headers);
  • perhapscwk
    New Member
    • Sep 2007
    • 123

    #2
    anyone can help? thanks.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      don’t know.

      I use the SwiftMailer library for everything that is more than a simple text mail.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Dormilich
        don’t know.

        I use the SwiftMailer library for everything that is more than a simple text mail.
        I second this. PHP's mail() function sucks. Check out SwiftMailer.

        Comment

        Working...