0

I'm quite new to PHP and I needed a contact form so I took one off the internet and edited everything I needed/wanted too. The mail won't send though, so if anyone could help me fix this I would be very happy!

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); 
    }
         }
    ?>
Bezod
  • 13
  • 1
  • 3

2 Answers2

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

should be

<input type="text" name="first_name" id="name" placeholder="Fullständigt namn" />
bansi
  • 55,591
  • 6
  • 41
  • 52
-2

I had to modify a few things in your form and PHP handler in order to get all the fields to match and work.

NOTE: You will need to modify a few things in your own language, but besides that, this worked for me and Email was in fact sent and received.

HTML form

<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 handler (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
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Strange... didn't work. Do you think the host could be the problem? – Bezod Aug 25 '13 at 14:11
  • @HermanLarsson It has to be, because I fully tested my answer before posting it, otherwise I wouldn't have done so. – Funk Forty Niner Aug 25 '13 at 14:12
  • Alrigh, thanks anyway. It still wouldn't have worked unless everyone here fixed up the code – Bezod Aug 25 '13 at 14:13
  • @HermanLarsson You're welcome Herman. If you're using this on a free hosted website, chances are that the `mail()` function has been disabled. Yet, I can assure you my fixed code does in fact work. – Funk Forty Niner Aug 25 '13 at 14:17
  • @HermanLarsson The closing brace at the beginning or end of your code does NOT make a difference whatsoever and will produce the exact same result. – Funk Forty Niner Aug 25 '13 at 14:23
  • I'm very sorry, I mistakly unaccept your answer and then I accepted it again. I didn't downvote your answer, I have too low reputation as I joined today. I am grateful that you put down time to help me with my code. Thanks! – Bezod Aug 25 '13 at 14:34
  • @Fred-ii-, Herman has 3 reputation and cannot vote on your answer at all. Don’t be rude, please. – Ry- Aug 25 '13 at 16:07
  • Yeah, really strange how my **correct** answer is being downvoted. Meh~ – Funk Forty Niner Aug 25 '13 at 17:22