0

I copied the message form and PHP mail from a website. But it doesn't seem to work. It does not send anything or make any reaction. I tried to find the error, but I am not familiar with PHP. I tried editing the $emailFrom =... to $_POST['email']; but that doesn't work either..

HTML:

<div id="form-main">
            <div id="form-div">
                <form class="form" id="form1">
                    <p class="name">
                        <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Naam"/>
                    </p>
                    <p class="email">
                        <input name="email" type="text" class="validate[required,custom[email]] feedback-input" placeholder="E-mail" />
                    </p>
                    <p class="text">
                        <textarea name="text" class="validate[required,length[6,300]] feedback-input" placeholder="Bericht"></textarea>
                    </p>
                    <div class="submit">
                        <input type="submit" value="Verstuur" id="button-blue"/>
                    <div class="ease"></div>
                    </div>
                </form>
            </div>
        </div>

PHP:

    <?php

include 'functions.php';

if (!empty($_POST)){

  $data['success'] = true;
  $_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
  $_POST  = multiDimensionalArrayMap('cleanData', $_POST);

  //your email adress 
  $emailTo ="lisa-ederveen@hotmail.com"; //"yourmail@yoursite.com";

  //from email adress
  $emailFrom =$_POST['email']; //"contact@yoursite.com";

  //email subject
  $emailSubject = "Mail from Porta";

  $name = $_POST["name"];
  $email = $_POST["email"];
  $comment = $_POST["comment"];
  if($name == "")
   $data['success'] = false;

 if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) 
   $data['success'] = false;


 if($comment == "")
   $data['success'] = false;

 if($data['success'] == true){

  $message = "NAME: $name<br>
  EMAIL: $email<br>
  COMMENT: $comment";


  $headers = "MIME-Version: 1.0" . "\r\n"; 
  $headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
  $headers .= "From: <$emailFrom>" . "\r\n";
  mail($emailTo, $emailSubject, $message, $headers);

  $data['success'] = true;
  echo json_encode($data);
}
}
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268

2 Answers2

2

Your form was incomplete, it missed the method (POST) and action (your php filename)

Try this instead:

<div id="form-main">
            <div id="form-div">
                <form action="sendEmail.php" method="POST" class="form" id="form1">
                    <p class="name">
                        <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Naam"/>
                    </p>
                    <p class="email">
                        <input name="email" type="text" class="validate[required,custom[email]] feedback-input" placeholder="E-mail" />
                    </p>
                    <p class="text">
                        <textarea name="comment" class="validate[required,length[6,300]] feedback-input" placeholder="Bericht"></textarea>
                    </p>
                    <div class="submit">
                        <input type="submit" value="Verstuur" id="button-blue"/>
                    <div class="ease"></div>
                    </div>
                </form>
            </div>
        </div>

sendEmail.php

    <?php

//include 'functions.php';

if (!empty($_POST)){

  $data['success'] = true;
  //$_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
  //$_POST  = multiDimensionalArrayMap('cleanData', $_POST);

  //your email adress 
  $emailTo ="lisa-ederveen@hotmail.com"; //"yourmail@yoursite.com";

  //from email adress
  $emailFrom =$_POST['email']; //"contact@yoursite.com";

  //email subject
  $emailSubject = "Mail from Porta";

  $name = $_POST["name"];
  $email = $_POST["email"];
  $comment = $_POST["comment"];
  if($name == "")
   $data['success'] = false;

 if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) 
   $data['success'] = false;


 if($comment == "")
   $data['success'] = false;

 if($data['success'] == true){

  $message = "NAME: $name<br>
  EMAIL: $email<br>
  COMMENT: $comment";


  $headers = "MIME-Version: 1.0" . "\r\n"; 
  $headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
  $headers .= "From: <$emailFrom>" . "\r\n";
  mail($emailTo, $emailSubject, $message, $headers);

  $data['success'] = true;
  echo json_encode($data);
}
}
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
2

Firstly, your form: <form class="form" id="form1">

Forms default to GET if a method isn't specifically instructed.

Use POST like this if your HTML form and PHP are inside the same file:

<form class="form" id="form1" method="post">

since you are using POST arrays.

or

<form class="form" id="form1" method="post" action="your_handler.php">

if using a different file; I used your_handler.php as an example filename.

Also, <textarea name="text"...

that should be <textarea name="comment" as per your $_POST["comment"] array.

  • Using error reporting would have trigged an Undefined index text... notice.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

I have no idea what multiDimensionalArrayMap('cleanEvilTags' does, so you'll have to check that.

If you're still not receiving mail, check your Spam.

Doing:

if(mail($emailTo, $emailSubject, $message, $headers)){
   echo "Mail sent.";
}

and if it echoes "Mail sent", then mail() would have done its job. Once it goes, it's out of your hands.

You could look into using PHPMailer or Swiftmailer which are better solutions, as is using SMTP mailing.

Now, if (!empty($_POST)){ that isn't a full solution. It is best using a conditional !empty() for all your inputs. Your submit counts as a POST array and should only be relied on using an additional isset() for it.

  • If you're using this from your own computer, make sure that you've a Webserver installed. We don't know how your script is being used.

If you are using it from your own machine, make sure that PHP is indeed running, properly installed and configured, including any mail-related settings.


Additional notes:

You should also use full and proper bracing for all your conditional statements.

This has none:

if($comment == "")
   $data['success'] = false;

which should read as

if($comment == ""){
   $data['success'] = false;
  }

Same thing for:

if($name == "")
   $data['success'] = false;
  • Not doing so, could have adverse effects.

"I copied the message form and PHP mail from a website."

Again, about multiDimensionalArrayMap('cleanEvilTags'; if you don't have that function, then you will need to get rid of it and use another filter method for your inputs.

Consult the following on PHP.net for various filter options:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141