联系表格(自制,没有图书馆)除了重新加载网站之外不发送任何内容

时间:2017-08-10 07:12:35

标签: php contact-form

我是PHP初学者,我正努力让这个联系表格在这个网站上运行:http://bm-translations.de/#kontakt 它重新加载页面,当我按下发送并且它什么也没发送。我究竟做错了什么? 这是我的代码:

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");




if(isset($_POST) && !empty($_POST)) {
    // catch spam bots which never loaded contact form
   // if (!isset($_POST["url"]) || $_POST["url"] != "sometext") {

      //  header("Location: http://bm-translations.de/");
     //   exit;

   // }

    // check whether the POST method was used
    if ("POST" != getenv("REQUEST_METHOD")) {

        header("Location: http://bm-translations.de/");
        exit;

    } 
    // check for user-agent and http-referer
    if ("" == getenv("HTTP_USER_AGENT") || "" == getenv("HTTP_REFERER")) {
        header("Location: http://bm-translations.de/");
        exit;

    } 
    //  trick the spam bot into identifying itself using a honeypot
    if (empty($_POST["email"])) {
        exit;

    }
    $hasAttachment = !empty($_FILES['attachment']['name']);
    $uid = md5(uniqid(time()));
    //mail essentials
    $from = $_POST['mail'];
    $to = "krystian.manthey@gmx.de";
    $subject = "Anfrage über Website";
    $message = $_POST['quote'];
    // standard mail headers
    $header = "From: ".$from."\r\n";
    $header .= "Reply-To: ".$to."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $message .= "This is a multi-part message in MIME format.\r\n";
    // plain text part
    $message .= "--".$uid."\r\n";
    $message .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $message .= $message."\r\n\r\n";
    //if there is an attachment
    if($hasAttachment) {
        //store some varables
        $file_name = $_FILES['attachment']['name'];
        $temp_name = $_FILES['attachment']['tmp_name'];
        $file_type = $_FILES['attachment']['type'];
        //get the extension of the file
        $base = basename($file_name);
        $extension = substr($base, strlen($base)-4, strlen($base));
        // only thes file types will be allowed
        $allowed_extensions = array(".doc","docx",".pdf",".zip",".png","jpeg",".jpg",".gif",".txt","docm",".odt","xlsx","xlsm",".csv",".xml",".ods","tiff",".rtf","");
        // check that this file type is allowed
        if(in_array($extension,$allowed_extensions)){
            // things you need
            $file = $temp_name;
            $content = chunk_split(base64_encode(file_get_contents($file)));
            // file attachment
            $message .= "--".$uid."\r\n";
            $message .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
            $message .= "Content-Transfer-Encoding: base64\r\n";
            $message .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
            $message .= $content."\r\n\r\n";
        }
    } 
    $msg = "";
    //send the mail 
    if (mail($to, $subject, $message, $header)) {
        $msg = "Ich habe Ihre Mail erhalten und melde mich in Kürze!";
    } else {
        $msg = "Nachricht konnte nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
    }
}

多数表格:

<?php if (!empty($msg)) {
                    echo "<h2>$msg</h2>";
                } ?>
                <form method="POST" enctype="multipart/form-data">
                    <input style="width: 100%;" type="text" name="name" id="name" placeholder="Ihr Name..." required/><br>
                    <input style="width: 100%;" type="email" name="email" id="email" placeholder="Ihre Email-Adresse..." required/><br>
                    <input style="width: 100%;" type="text" name="phone" id="phone" placeholder="Ihre Telefonnummer..." /><br>
                    <textarea style="padding: 10px 12px; width: 100%;" name="quote" rows="6" placeholder="Auftragsdetails: Fachgebiet, Sprachkombination, Beglaubigung (ja/nein), Liefertermin, Versandart ..." required></textarea><br>
                    <input name="uploaded_file" id="uploaded_file" type="file" size="50" style="margin-top: 10px;">
                    <input name="url" style="display:none">
                    <button type="submit" class="button" name="submit" value="send" style="width:100%; margin-top:10px; text-align: center;">Anfrage absenden!</button>
                </form>

现在我收到此消息 not really the information I sent

和很多错误,我错了什么?我试着按照答案的说明。 enter image description here

1 个答案:

答案 0 :(得分:1)

更新5 ..............

error_reporting(0);


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

    // catch spam bots which never loaded contact form
   // if (!isset($_POST["url"]) || $_POST["url"] != "sometext") {

      //  header("Location: http://bm-translations.de/");
     //   exit;

   // }

// check whether the POST method was used
if ("POST" != getenv("REQUEST_METHOD")) {

    echo "error 1";
    exit;

} 
// check for user-agent and http-referer
if ("" == getenv("HTTP_USER_AGENT") || "" == getenv("HTTP_REFERER")) {
echo "Error 2";
exit;

} 
//  trick the spam bot into identifying itself using a honeypot
if (empty($_POST["email"])) {
exit;

}
$hasAttachment = !empty($_FILES['uploadedfile']['name']);
$uid = md5(uniqid(time()));
//mail essentials
$from = "yoursendingaccount@email.com";
$to = "recevingemail@email.com";
$subject = "Your subject";
$message2 = $_POST['quote']."\r\n";
$message2 .= 'Email provided is '.$_POST['email'].'\r]n';
// standard mail headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$to."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$message = "This is a multi-part message in MIME format.\r\n";
// plain text part
$message .= "--".$uid."\r\n";
$message .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $message2."\r\n\r\n";
//if there is an attachment
if($hasAttachment) {
    //store some varables
    $file_name = $_FILES['uploadedfile']['name'];
    $temp_name = $_FILES['uploadedfile']['tmp_name'];
    $file_type = $_FILES['uploaded']['type'];
    //get the extension of the file
    $base = basename($file_name);
    $extension = substr($base, strlen($base)-4, strlen($base));
    // only thes file types will be allowed
    $allowed_extensions = array(".doc","docx",".pdf",".zip",".png","jpeg",".jpg",".gif",".txt","docm",".odt","xlsx","xlsm",".csv",".xml",".ods","tiff",".rtf","");
    // check that this file type is allowed
    if(in_array($extension,$allowed_extensions)){
        // things you need
        $file = $temp_name;
        $hd="/home2/inam/public_html/uploads/";
        $uploaddir=$hd.$file_name;
        copy($temp_name,$uploaddir);
        $content = chunk_split(base64_encode(file_get_contents($uploaddir)));
        // file attachment

        $message .= "--".$uid."\r\n";


    $message .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
            $message .= "Content-Transfer-Encoding: base64\r\n";
            $message .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
            $message .= $content."\r\n\r\n";
        }
    } 
    $msg = "";
    //send the mail 
    if (mail($to, $subject, $message, $header)) {
        $msg = "Mail is sent";
    } else {
        $msg = "Email was not sent";
    }
}
?>
<?php if (!empty($msg)) {
                    echo "<h2>$msg</h2>";
                } ?>
            <form method="POST" enctype="multipart/form-data">
                <input style="width: 100%;" type="text" name="name" id="name" placeholder="Name" required/><br>
                <input style="width: 100%;" type="email" name="email" id="email" placeholder="email" required/><br>
                <input style="width: 100%;" type="text" name="phone" id="phone" placeholder="phone" /><br>
                <textarea style="padding: 10px 12px; width: 100%;" name="quote" rows="6" placeholder="Description" required></textarea><br>
                <input name="uploadedfile" id="uploadedfile" type="file" size="50" style="margin-top: 10px;">
                <input name="url">
                <button type="submit" class="button" name="submit" value="send" style="width:100%; margin-top:10px; text-align: center;">Anfrage absenden!</button>
            </form>

更新4.就像我想的那样。问题是if (!empty($_POST["email"])) { exit; }

只需将其更改为if (empty($_POST["email"])) { exit; }

即可

并且您的表单有效。我再次检查了它,只删除了$ _POST [&#39; p3&#39;]因为我不知道它是什么。也命名为按钮。 <button name=button...

更新3.好的我试过你的表单删除了我不明白的原因,为什么首先存在,我可以收到包含附件的电子邮件。 在开头使用if(isset($_POST['submit'])){而不是if(isset($_POST))

然后我删除了$ _POST [&#39; p3&#39;]因为我不知道它在表单中的位置,我也通过输入if (!empty($_POST["email"])) { exit; } //////删除了你基本上是说如果电子邮件不是空的退出。它应该是if(empty($_POST['email']))但我删除它只是为了检查您的表单。

尝试这些更改。

更新2.您的文件输入为<input name="uploaded_file" id="uploaded_file" type="file" size="50" style="margin-top: 10px;"> 但你正在使用$hasAttachment = !empty($_FILES['attachment']['name']); 将其更改为$hasAttachment = !empty($_FILES['uploaded_file']['name']);

我试过你的表格,但据我所知,这不是整个表格。 $ _POST [&#34; p3&#34;]在哪里。无论如何,请尝试我上面提到的解决方案。

您没有输入按钮的名称。

尝试这个

<button type="submit" class="button" name=submit value="send" style="width:100%; margin-top:10px; text-align: center;">Anfrage absenden!</button>