Drupal - 自定义表单提交

时间:2013-05-01 15:09:30

标签: php html drupal

我知道0关于drupal,遗憾的是对模块和类似事情没有多少控制权。

那就是说...我需要让表单向收件人提交一封电子邮件,这是一个非常标准的表单,附带文件附件。我有一个为我预先编写的脚本,并确认在一个单独的PHP站点上工作。我遇到了一些在drupal上使用它的问题。基本上我在提交表单时得到405 Not Noted错误...我们有一个跟踪软件,通常处理我们的表单提交,遗憾的是它无法处理文件附件,所以我必须使用自定义PHP脚本才能发送电子邮件。该脚本以与我们的跟踪软件配对的方式制作,以便我们仍然可以跟踪表单提交。

代码所在的页面设置为类型的PHP代码。 (而不是完整的html或过滤的html)。服务器上的文件暂时是chmod 777 - 虽然我只需要666吧?

为什么我收到405 Not Noted错误?

以下是一些代码:

表单的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 mailfile:

<?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 = "mhostiuckproductions@gmail.com";
    //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.");

?>

2 个答案:

答案 0 :(得分:0)

如果您要使用现有代码,Form Api和自定义模块可能就是您的选择。但是如果你可以安装一些模块Webform,那么其他一些模块应该通过配置生成这个功能,而且几乎没有代码。

您使用此功能的方式并不是drupal期望如何创建表单,因此您可能会继续遇到此模式的问题。 More info on drupal forms

P.S。你应该包括你的drupal版本。

答案 1 :(得分: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

Alex建议的

Webform可能适合您,但我不确定它是否提供了文件附件功能,这是您要求的一部分。