2011-12-07 73 views
0

我在嘗試讓我的腳本發送文件附件與從表單收集的信息時遇到了一些麻煩。我用PHP Mailer Form with File Attachment和修改,以適應我的規範。到目前爲止,它將發送沒有附加圖像的表單信息,但是當我上傳圖像時,它只會發送附件並且不會從表單發送其他信息。我需要它能夠發送帶或不帶附件的表格信息。我是一個初學者與PHP,所以我不知道爲什麼它這樣做。需要帶有文件附件腳本的PHP Mailer表單發送帶或不帶附件的表單信息

這裏是我的代碼

<?php 

if(isset($_POST['submit'])){ 

    $sendTo = '<[email protected]'; 
    $subject = 'Request'; 
    $from = $_POST['email_address']; 

    // Allowed file types. add file extensions WITHOUT the dot. 
    $allowtypes=array("jpg", "png", "gif"); 

    // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached 
    $requirefile="true"; 

    // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it, 
    // post_max_size, upload_max_filesize, file_uploads, max_execution_time! 
    // 2048kb = 2MB,  1024kb = 1MB,  512kb = 1/2MB etc.. 
    $max_file_size="5120"; 

    $errors = array(); //Initialize error array 

    //Check if the file type uploaded is a valid file type. 
    if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) { 
      // basename -- Returns filename component of path 
      $filename = basename($_FILES['attachment']['name']); 
      $ext = substr($filename, strrpos($filename, '.') + 1); 
      $filesize=$_FILES['attachment']['size']; 
      $max_bytes=$max_file_size*5120; 

      //Check if the file type uploaded is a valid file type. 
      if (!in_array($ext, $allowtypes)) { 
       $errors[]="Invalid extension for your file: <strong>".$filename."</strong>"; 

     // check the size of each file 
     } elseif($filesize > $max_bytes) { 
       $errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb."; 
      } 

    } // if !empty FILES 

     // send an email 
     // Obtain file upload vars 
     $fileatt  = $_FILES['attachment']['tmp_name']; 
     $fileatt_type = $_FILES['attachment']['type']; 
     $fileatt_name = $_FILES['attachment']['name']; 

     // Headers 
     $headers = 'From: '.$from = $_POST['email_address']; 

     // create a boundary string. It must be unique 
     $semi_rand = md5(time()); 
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

     // Add the headers for a file attachment 
     $headers .= "\nMIME-Version: 1.0\n" . 
        "Content-Type: multipart/mixed;\n" . 
        " boundary=\"{$mime_boundary}\""; 

     // Add a multipart boundary above the plain message 
     $message ="This is a multi-part message in MIME format.\n\n"; 
     $message.="--{$mime_boundary}\n"; 
     $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
     $message.="Content-Transfer-Encoding: 7bit\n\n"; 
     $message .= "Name: \t\t".$_POST['name']."\n"; 
     $message .= "Email: \t\t".$_POST['email_address']."\n"; 
     if($_POST['phone_number'] != ""){ 
      $message .= "Phone: \t\t".$_POST['phone']."\n"; 

     $message .= "Date: \t\t".$_POST['full_date']."\n"; 
     } 
     $message .= "\n"; 
     if($_POST['additional_info'] != ""){ 
      $message .= "Additional Information: \n".$_POST['additional_info']."\n"; 
     } 
     if (is_uploaded_file($fileatt)) { 
     // Read the file to be attached ('rb' = read binary) 
     $file = fopen($fileatt,'rb'); 
     $data = fread($file,filesize($fileatt)); 
     fclose($file); 

     // Base64 encode the file data 
     $data = chunk_split(base64_encode($data)); 

     // Add file attachment to the message 
     $message .= "--{$mime_boundary}\n" . 
        "Content-Type: {$fileatt_type};\n" . 
        " name=\"{$fileatt_name}\"\n" . 
        //"Content-Disposition: attachment;\n" . 
        //" filename=\"{$fileatt_name}\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . 
        $data . "\n\n" . 
        "--{$mime_boundary}--\n"; 
     } 

     // Send the completed message 
     mail($sendTo, $subject, $message, $headers); 

     header("Location: complete.php"); 
} 

?> 
+0

無碼,沒人能幫到你。 – 2011-12-07 02:55:16

回答

0

這是我做了發送郵件帶有附件。有些代碼是我的,其中一些代碼來自http://php.net/manual/en/function.mail.php。此代碼已經過嘗試和測試,因此它應該可以工作。還看看是否安裝了sendmail。如果在linux的ubuntu系統上試試sudo apt-get install sendmail來安裝它。

文件名:index.php文件

<?php 
    if($_POST || $_FILES) 
    { 
      // email fields: to, from, subject, and so on 
      // Here 
      $from = "[email protected]"; 
      $to = "[email protected]_else.com"; 
      $subject = "Mail with Attachment"; 
      $message = "This is the message body and to it I will append the attachments."; 
      $headers = "From: $from"; 

      // boundary 
      $semi_rand = md5(time()); 
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

      // headers for attachment 
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

      // multipart boundary 
      $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
      fixFilesArray($_FILES['attachment']); 
      foreach ($_FILES['attachment'] as $position => $file) 
      { 
        // should output array with indices name, type, tmp_name, error, size 
        $message .= "--{$mime_boundary}\n"; 
        $fp  = @fopen($file['tmp_name'],"rb"); 
        $data = @fread($fp,filesize($file['tmp_name'])); 
        @fclose($fp); 
       $data = chunk_split(base64_encode($data)); 
       $message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      } 
      $message .= "--{$mime_boundary}--"; 
      $returnpath = "-f" . $from; 
      $ok = @mail($to, $subject, $message, $headers, $returnpath); 
      if($ok){ return 1; } else { return 0; } 
    } 
    //This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important 

    function fixFilesArray(&$files) 
    { 
      $names = array('name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1); 

      foreach ($files as $key => $part) { 
        // only deal with valid keys and multiple files 
        $key = (string) $key; 
        if (isset($names[$key]) && is_array($part)) { 
          foreach ($part as $position => $value) { 
            $files[$position][$key] = $value; 
          } 
          // remove old key reference 
          unset($files[$key]); 
        } 
      } 
    } 
    ?> 
    <html> 
     <body> 
      <form method="POST" action="index.php" enctype="multipart/form-data"> 
       <input type="file" name="attachment[]"><br/> 
       <input type="submit"> 
      </form> 
     </body> 
    </html> 

通過我願意爲使用sendmail,因爲它是慢了一點道歉的方式。我會嘗試發佈更好的解決方案。

+0

謝謝Jay。我會試一試,讓你知道它是如何發展的。 – IncI

+0

周杰倫謝謝。你的代碼可以幫助我解決我遇到的問題。 – IncI

+0

我怎樣才能限制這個腳本只接受某些擴展名,如.PDF和.DOC/.DOCX?謝謝 – Interfaith