电子邮件不会从我的PHP / html联系表单发送

时间:2013-08-25 13:28:03

标签: php html contact-form

我对PHP很陌生,我需要一个联系表单,所以我从网上拿了一个并编辑了我需要/想要的一切。邮件不会发送,所以如果有人能帮我解决这个问题,我会非常高兴!

HTML:

        <form name="contactform" method="post"action="send_form_email.php">
        <p class="name">
        <input type="text" name="first_name" id="name" placeholder="Fullständigt namn" />
        </p>

        <p class="foretag">
        <input type="text" name="foretag" id="foretag" placeholder="Företag" />
        </p>

        <p class="email">
        <input type="text" name="email" id="email" placeholder="Email" />
        </p>

        <p class="telefon">
        <input type="text" name="telephone" id="telephone" placeholder="Telefonnummer" />
        </p>        

        <p class="text">
        <textarea name="text" id="text" placeholder="Meddelande" /></textarea>
        </p>

        <p class="robotic" id="pot">
        <label>Om du är människa, fyll inte i denna ruta:</label>
        <input name="robotest" type="text" id="robotest" class="robotest" />
        </p>

        <p class="submit">
        <input type="submit" value="Skicka" />
        </p>
        </form>
</div>

PHP:

    <?php
    if(isset($_POST['email'])) {


        $email_to = "EMAIL HERE";
        $email_subject ="subject";


        function died($error) {
            // your error code can go here
            echo "Fel uppstog";
            echo "Error kod:.<br /><br />";
                 echo $error."<br /><br />";
            echo "Gå tillbaka och rätta till problemen innan du försöker igen.<br /><br />";
            die();
        }

        // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['foretag']) ||
            !isset($_POST['email']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['text'])) {
            died('Det verkar vara ett problem med formuläret du angav');       
        }

        $first_name = $_POST['first_name'];
        $foretag = $_POST['foretag'];
        $email = $_POST['email'];
        $telephone = $_POST['telephone'];
        $text = $_POST['text'];

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email)) {
        $error_message .= 'Emailadressen du angav verkar inte vara giltig.<br />';
      }
        $string_exp = "/^[A-Za-z .'-]+$/";
      if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'Namnet du angav verkar inte vara giltig.<br />';
      }
      if(!preg_match($string_exp,$foretag)) {
        $error_message .= 'Företaget du angav är inte giltig..<br />';
      }
      if(strlen($text) < 2) {
        $error_message .= 'Meddelandet du angav är inte giltig..<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }
        $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "First Name: ".clean_string($first_name)."\n";
        $email_message .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";

if (preg_match("/http/i", "$first_name")) {echo "$spamErrorMessage"; exit();}
        if (preg_match("/http/i", "$email")) {echo "$spamErrorMessage"; exit();}
        if (preg_match("/http/i", "$telephone")) {echo "$spamErrorMessage";exit();}
        if (preg_match("/http/i", "$text")) {echo "$spamErrorMessage"; exit();}

         $SpamErrorMessage = "Ingen URL tillåten";

         $headers = 'From: '.$email_from."\r\n";
         'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();


        if($robotest)
            $error = "You are a gutless robot.";
        else {
    @mail($email_to, $email_subject, $email_message, $headers); 
    }
         }
    ?>

2 个答案:

答案 0 :(得分:0)

<input type="text" name="name" id="name" placeholder="Fullständigt namn" />

应该是

<input type="text" name="first_name" id="name" placeholder="Fullständigt namn" />

答案 1 :(得分:-1)

我必须修改表单和PHP处理程序中的一些内容,以便让所有字段匹配并运行。

注意:您需要使用自己的语言修改一些内容,但除此之外,这对我有用,而电子邮件实际上已发送和接收。

HTML表单

<form name="contactform" method="post" action="send_form_email.php">
    <p class="name">
    <input type="text" name="first_name" id="first_name" placeholder="Fullständigt namn" />
    </p>

    <p class="last_name">
    <input type="text" name="last_name" id="last_name" placeholder="Last name" />
    </p>

    <p class="foretag">
    <input type="text" name="foretag" id="foretag" placeholder="Företag" />
    </p>

    <p class="email">
    <input type="text" name="email_from" id="email" placeholder="Email" />
    </p>

    <p class="telefon">
    <input type="text" name="telephone" id="telephone" placeholder="Telefonnummer" />
    </p>        

    <p class="text">
    <textarea name="text" id="text" placeholder="Meddelande" /></textarea>
    </p>

    <p class="robotic" id="pot">
    <label>Om du är människa, full inte i denna ruta:</label>
    <input name="robotest" type="text" id="robotest" class="robotest" />
    </p>

    <p class="submit">
    <input type="submit" value="Skicka" />
    </p>
    </form>
</div>

PHP处理程序(send_form_email.php)

<?php
    if(isset($_POST['email_from'])) {


        $email_to = "email@example.com";
        $email_subject = "bezod-design kontakta";

         // } this could be used, didn't make much of a difference.
         // closing brace for this is commented at the end of this code
         // but you will need to delete the closing brace at the end if using the closing brace here
        function died($error) {
            // your error code can go here
            echo "Erros uppstog";
            echo "Dem/det är listade nedan.<br /><br />";
                 echo $error."<br /><br />";
            echo "Gå tillbaks och fix dem innan du skickar.<br /><br />";
            die();
        }

        // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['last_name']) ||
            !isset($_POST['foretag']) ||
            !isset($_POST['email_from']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['text'])) {
            died('Det värkar vara ett problem med formuläret du skrev');       
        }

        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $foretag = $_POST['foretag'];
        $email_from = $_POST['email_from'];
        $telephone = $_POST['telephone'];
        $text = $_POST['text'];

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'Email adressen du skrev värkar inte vara giltig.<br />';
      }
        $string_exp = "/^[A-Za-z .'-]+$/";
      if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'Namnet adressen du skrev värkar inte vara giltig.<br />';
      }
      if(!preg_match($string_exp,$foretag)) {
        $error_message .= 'Företaget du skrev värkar inte vara giltig..<br />';
      }
      if(strlen($text) < 2) {
        $error_message .= 'Meddelandet du skrev värkar inte vara giltig..<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }
        $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "First Name: ".clean_string($first_name)."\n";
        $email_message .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Foretag: ".clean_string($foretag)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
        $email_message .= "Comments: ".clean_string($text)."\n";

if (preg_match("/http/i", "$first_name")) {echo "$spamErrorMessage"; exit();}
        if (preg_match("/http/i", "$email_from")) {echo "$spamErrorMessage"; exit();}
        if (preg_match("/http/i", "$telephone")) {echo "$spamErrorMessage";exit();}
        if (preg_match("/http/i", "$text")) {echo "$spamErrorMessage"; exit();}

         $SpamErrorMessage = "Ingen URL tillåten";

         $headers = 'From: '.$email_from."\r\n";
         'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();


        if($robotest)
            $error = "You are a gutless robot.";
        else {
    mail($email_to, $email_subject, $email_message, $headers); 
    echo "Message sent.";
        }
    } // closing brace for if(isset at beginning of handler
     // delete this brace if you are to use the closing one at the beginning
?>