Page 1 of 1

Email with multiple attachments

Posted: Tue Dec 16, 2008 11:05 am
by relief8
Hi,
I am trying to create a form that will send an html email with multiple attachments. My problem is that instead of one email with multiple attachments I am getting one email per attachment. I am pasting the code. Any help would be greatly appreciated.

Thanks

Code: Select all

 
//Loop to find all attachments
foreach ($_FILES["filename"]["error"] as $key => $error){
   if ($error == UPLOAD_ERR_OK) {
    
        //check that to and from are email addresses
    //-----------------------------------------------------------------------------------------------
    if(!isValidEmail($to) || !isValidEmail($from)) {
        echo "We're sorry, we can't email this cap yet. Email address invalid format, must be user@website format.<br/>";
        echo "TO: " . $to . "<BR>"; 
        echo "FROM: " . $from . "<BR>";
        exit;
    }
    //-----------------------------------------------------------------------------------------------
    
    //-----------------------------------------------------------------------------------------------
    //make the image, and output it to disk, under the temp directory
    //$image=imagecreatetruecolor( $width ,$height );
    //$background = imagecolorallocate( $image ,0 , 0 , 0 );
    //Copy pixels
    //$i = 0;
    //for($x=0; $x<=$width; $x++){
        //for($y=0; $y<=$height; $y++){
        //  $int = hexdec($data[$i++]);
        //  $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
        //  imagesetpixel ( $image , $x , $y , $color );
    //  }
//  }
    //save the new image to the image directory, named $refnum.jpg, with quality level 100%
//  ImageJPEG($image, IMG_PATH . $refnum . ".jpg", 100);
    
    //manual garbage collection:
//  imagedestroy($image);   
    //-----------------------------------------------------------------------------------------------
    
    
    //This is what is outputted to the browser, and the email message body
    $message = file_get_contents(TEMPLATE);
    
    //replace the fields    
    $message = str_replace("[CSQ", $cstmQuantity_2, $message);
    $message = str_replace("[CS", $cstmStyle_2, $message);
    $message = str_replace("[CCC", $cstmCrownColor_2, $message);
    $message = str_replace("[CVC", $cstmVisorColor_2, $message);
    $message = str_replace("[CFV2]", $flatVisor_2, $message);
    $message = str_replace("[CC2]", $cstmSandwichColor_2, $message);
    $message = str_replace("[CBC", $cstmButtonColor_2, $message);
    $message = str_replace("[CEC", $cstmEyeletColor_2, $message);
    $message = str_replace("[CP2]", $cstmPiping_2, $message);
    $message = str_replace("[CY", $cstmYouth_2, $message);
    $message = str_replace("[CS", $cstmSmall_2, $message);
    $message = str_replace("[CL", $cstmLarge_2, $message);
    $message = str_replace("[CA", $cstmAdjustable_2, $message);
    $message = str_replace("[C65]", $cstm658_2, $message);
    $message = str_replace("[C63]", $cstm634_2, $message);
    $message = str_replace("[C67]", $cstm678_2, $message);
    $message = str_replace("[C7]", $cstm7_2, $message);
    $message = str_replace("[C71]", $cstm718_2, $message);
    $message = str_replace("[C74]", $cstm714_2, $message);
    $message = str_replace("[C73]", $cstm738_2, $message);
    $message = str_replace("[C72]", $cstm712_2, $message);
    $message = str_replace("[C75]", $cstm758_2, $message);
    $message = str_replace("[C73]", $cstm734_2, $message);
    $message = str_replace("[C77]", $cstm778_2, $message);
    $message = str_replace("[C8]", $cstm8_2, $message);
    
    
//____________________________________________________________________________________________________
//Create attachment
   
   
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
   
   
   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'][$key];
   $type = $_FILES['filename']['type'][$key];
   $name = $_FILES['filename']['name'][$key];
   $size = $_FILES['filename']['size'][$key];
   
 
   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){
 
      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){
 
         // open the file for a binary read
         $file = fopen($tmp_name,'rb');
 
         // read the file content into a variable
         $data = fread($file,filesize($tmp_name));
 
         // close the file
         fclose($file);
 
         // now we encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
         
         }
 
 
      // now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";
 
      // next, we'll build the message body
      // note that we insert two dashes in front of the
      // MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";
         
      // now we'll insert a boundary to indicate we're starting the attachment
      // we have to specify the content type, file name, and disposition as
      // an attachment, then add the file content and set another boundary to
      // indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";
         
         
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Your message has been sent.  Thank you for the order.  Please see below for order details.";
      else
         echo "Failed to send";
   }
}
 
}
}
 
//---------------------------------------------------------
function isValidEmail($email) {
    // First, we check that there's one @ symbol, and that the lengths are right
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
        // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        return false;
    }
    
    // Split it into sections to make life easier
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) {
        if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
            return false;
        }
    }
    
    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) {
            return false; // Not enough parts to domain
        }
    
        for ($i = 0; $i < sizeof($domain_array); $i++) {
            if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
                return false;
            }
        }
    }
    return true;
}

Re: Email with multiple attachments

Posted: Tue Dec 16, 2008 2:10 pm
by John Cartwright
Duplicate. Locked.