2013-05-01 43 views
0

我知道0關於drupal,不幸的是沒有太多的控制模塊和類似的東西。Drupal - 自定義表單提交

這就是說...我需要做一個表單提交一封電子郵件給收件人,這是一個非常標準的形式,附帶一個文件。我有一個爲我預先編寫的腳本,並已確認在單獨的PHP網站上工作。但是,我遇到了一些在drupal上使用它的問題。基本上,我在提交表單時遇到了405不允許的錯誤...我們有一個跟蹤軟件,通常處理我們的表單提交,不幸的是它無法處理文件附件,所以我必須使用自定義PHP腳本才能發送電子郵件。腳本是以這種方式與我們的跟蹤軟件配對,以便我們仍然可以跟蹤表單提交。

代碼所在的頁面被設置爲類型的PHP代碼。 (而不是完整的HTML,或過濾的HTML)。服務器上的文件暫時是chmod 777 - 雖然我應該只需要666吧?

爲什麼我得到這405不允許的錯誤?

下面是一些代碼:

HTML的形式:

<form id="contact-form" action="/sites/www.mathistire.com/files/mailfile-job.php" method="post" enctype="multipart/form-data"> 
<table> 

    <tr> 
     <td><label title="Name">Name:</label></td><td><input type="text" name="Name" /></td> 
    </tr> 
    <tr> 
     <td><label title="E-Mail">E-Mail:</label></td><td><input type="email" name="EMail" /></td> 
    </tr> 
    <tr> 
     <td><label title="Position">Position:</label></td><td><input type="text" name="Position" /></td> 
    </tr> 
    <tr> 
     <td><label title="Attach">Attach File:</label></td><td><input type="file" name="import_file" alt="import_file" title="import_file" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"><input type="submit" name="Submit" /></td> 
    </tr> 

</table> 
</form> 

PHP郵件文件:

<?php 
ini_set('upload_max_filesize','24M'); 
ini_set('post_max_size','32M'); 

if ($_POST) { 
    $field_tracking = ""; 
    $now = date("D m/d/Y H:i:s e"); 

    // case normalized list of field names that we don't want to encapsulate in xml 
    $metafieldnames = array("submit","imemailsubject","imredirect","formname","imdefaultrecipient","import_file","max_file_size","x","y"); 

    // build the email message from the list of fields list 
    $emailxtra="INFORMATION FROM WEB FORM: ".$_POST['formname'].": " . $now . "\n\n"; 
    //check for post values 
    while(list($key, $value) = each($_POST)) {  
     if(is_array($value)){ 
      $value = implode(', ',$value); 
     } 

     //add value to scope 
     $$key = $value; 

     // only encapsulate fields that are not metadata fields or submit button 
     if (!in_array (strtolower($key), $metafieldnames)) { 
      $emailxtra=$emailxtra.$key.": ".stripslashes($value)."\r\n\r\n"; 
      $field_tracking .= "\nurl += '&".$key."=' + escape('".addslashes($value)."');"; 
     }  
    } 
    //echo ('FIELD TRACKING: ' . $field_tracking); 


    //create the email 
    $mime_boundary = "<<<--==+X[".md5(time())."]"; 

    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "From: ".$imEmailField."\r\n"; 
    $headers .= "Reply-To: ".$EMail."\r\n"; 
    $headers .= "Content-Type: multipart/mixed;\r\n"; 
    $headers .= " boundary=\"".$mime_boundary."\""; 

    $mail_message = "This is a multi-part message in MIME format.\r\n\r\n"; 
    $mail_message .= "--".$mime_boundary."\r\n"; 
    $mail_message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
    $mail_message .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
    $mail_message .= $emailxtra."\r\n"; 


    $upload_error = ""; 
    if ($_FILES && count($_FILES) > 0) { 
     //upload file 1 
     if (is_uploaded_file($_FILES['import_file']['tmp_name'])) { 
      if(preg_match("/.exe$|.com$|.bat$|.rar$|.egs$/i", $_FILES['import_file']['name'])){ 
       $upload_error = "Attempted to upload .exe .com .rar .egs or .bat file."; 
      } 
     //echo ('file uploaded'); 
     } 

     else { 
     switch($_FILES['import_file']['error']){ 
      case 0: //no error; possible file attack! 
       $upload_error = "no error; possible file attack!"; 
       break; 
      case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini 
       $upload_error = "uploaded file exceeds the upload_max_filesize directive in php.ini."; 
       break; 
      case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form 
       $upload_error = "uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form."; 
       break; 
      case 3: //uploaded file was only partially uploaded 
       $upload_error = "uploaded file was only partially uploaded."; 
       break; 
      case 4: //no file was uploaded 
       $upload_error = "No file was uploaded."; 
       break; 
      default: //a default error, just in case! :) 
       $upload_error = "There was a problem with your upload."; 
       break; 
      } 
     } 
     if ($upload_error != "") { 
      $mail_message .= "ERROR with attachment 1 file ".$_FILES['import_file']['name'].": ".$upload_error."\r\n"; 
     } 
     else { 
      $mail_message .= "--".$mime_boundary."\r\n"; 
      $mail_message .= "Content-Type: application/octet-stream;\r\n"; 
      $mail_message .= " name=\"".$_FILES['import_file']['name']."\"\r\n"; 
      $mail_message .= "Content-Transfer-Encoding: base64\r\n"; 
      $mail_message .= "Content-Disposition: attachment;\r\n"; 
      $mail_message .= " filename=\"".$_FILES['import_file']['name']."\"\r\n"; 
      $mail_message .= "\r\n"; 
      $fp = fopen($_FILES['import_file']['tmp_name'],"r"); 
      $contents = fread ($fp, filesize($_FILES['import_file']['tmp_name'])); 
      fclose($fp); 
      $contents = chunk_split(base64_encode($contents)); 
      //echo $contents; 
      //$mail_message .= strip_tags($contents); 
      $mail_message .= $contents; 
      //$mail_messagee .= "\r\n"; 
      //$mail_message .= "--".$mime_boundary."\r\n"; 
     } 
    } //close check for files 


    $recipient = "mh[email protected]"; 
    //echo $mail_message; 
    mail($recipient,$subj_slug,$mail_message,$headers); 

} //close post check 

//Insite  
$url = "http://www.insitemetrics.com/imv2/uRMJ/uniformv2.php?actk=6vdsc0-5yfrjtbfcq" . 
"&Name=" . $_REQUEST['Name'] . 
"&EmailField=" . $_REQUEST['EMail'] . 
"&Position=" . $_REQUEST['Position'] . 
"&FileAttach=" . $_FILES['import_file']['name']; 



header("location:".$url); 
exit; 
//echo ("mail sent."); 

?> 

回答

0

Form Api和一個自定義模塊,將可能是要走的路,如果你」重新使用現有的代碼。但是,如果你可以安裝一些模塊Webform和其他幾個模塊應該配置和幾乎沒有代碼產生這個功能。

你對這個功能的看法並不是drupal希望創建表單,所以你可能會繼續遇到這種模式的問題。 More info on drupal forms

P.S.你應該包括你的drupal版本。

0

你不能直接在drupal中寫表單。

您需要在drupal中創建一個自定義模塊。
的Drupal 7:http://drupal.org/node/1074360
的Drupal 6:http://drupal.org/node/206753

一旦你知道如何編寫一個基本的模塊,那麼你需要了解程序添加自定義表單在Drupal或者您可以直接跳轉到該使用示例代碼這裏提供的,但你可能不明白是怎麼回事):

的Drupal 7:http://www.youtube.com/watch?v=syqsH2CEu6U/http://drupal.org/node/1043838
的Drupal 6:http://drupal.org/node/751826

Webform由Alex建議可能適用於您,但我不確定它是否提供文件附件功能,這是您的要求的一部分。