0

我对drupal的了解为0,不幸的是对模块和类似的东西没有太多控制。

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

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

为什么我会收到此 405 Not Allowed 错误?

这是一些代码:

表单的 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 = "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.");

?>
4

2 回答 2

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可能对您有用,但我不确定它是否提供文件附件功能,这是您要求的一部分。

于 2013-05-01T15:47:05.097 回答
0

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

您使用此功能的方式不是 drupal 期望创建表单的方式,因此您可能会继续遇到此模式的问题。有关 drupal 表单的更多信息

PS你应该包括你的drupal版本。

于 2013-05-01T15:35:00.337 回答