0

I have no idea why my scripts are not working as they should. I am using bootstrap form. When my form is filled and going to click Submit button then I am redirecting to empty webpage like and nothing is send:

http://mywebpageaddress.com/send_form_email.php?comments=Hi%2C%0D%0Afirst+message%0D%0AJohn

bootstrap index.html code form:

<form action="send_form_email.php" method="post" class="form-horizontal">
          <div class="form-group">
            <label for="first_name" class="col-lg-2 control-label">First name</label>
            <div class="col-lg-10">
            <input type="text" class="form-control" id="first_name" name="first_name" placeholder="Enter you first name">
            </div>
          </div><!-- End form group -->

           <div class="form-group">
            <label for="last_name" class="col-lg-2 control-label">Last name</label>
            <div class="col-lg-10">
              <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Enter you last name">
            </div>
          </div><!-- End form group -->

          <div class="form-group">
            <label for="email" class="col-lg-2 control-label">Email</label>
            <div class="col-lg-10">
              <input type="text" class="form-control" id="email" name="email" placeholder="Enter you Email Address">
            </div>
          </div><!-- End form group -->

          <div class="form-group">
            <label for="telephone" class="col-lg-2 control-label">Telephone</label>
            <div class="col-lg-10">
              <input type="text" class="form-control" id="telephone" name="telephone" placeholder="Enter you phone number">
            </div>
          </div>

          <div class="form-group">
            <label for="comments" class="col-lg-2 control-label">Any Message</label>
            <div class="col-lg-10">
              <textarea name="comments" id="comments" name="comments" class="form-control" 
              cols="20" rows="10" placeholder="Enter your Message"></textarea>
            </div>
          </div> 

          <div class="form-group">
            <div class="col-lg-10 col-lg-offset-2">
              <button type="submit" class="btn btn-primary">Submit</button>
            </div>
          </div>



        </form>

PHP file:

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

    // CHANGE THE TWO LINES BELOW
    $email_to = "myemailt@gmail.com";

    $email_subject = "website html form submissions";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $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 .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<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";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
die();
?>

output:

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

    // CHANGE THE TWO LINES BELOW
    $email_to = "myemailt@gmail.com";

    $email_subject = "website html form submissions";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $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 .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<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";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
die();
?>
Siguza
  • 21,155
  • 6
  • 52
  • 89
Arie
  • 3,041
  • 7
  • 32
  • 63
  • Might want to remove the `@` in `@mail` since it suppresses error messages. Also, decide whether you're using `POST` or `GET`. Default form method is `GET` but your PHP is expecting `POST`. – Huey Aug 01 '15 at 05:56

2 Answers2

2

You don't set method in your form element. so by default it set get. Change

<form action="send_form_email.php" method="post" class="form-horizontal">

And change your input fields and set name like:

<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Enter you first name">

And so on.

Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
  • I added this but after fill out form was again redirected to empty page: http://mywebpageaddress.com/send_form_email.php – Arie Aug 01 '15 at 06:05
  • OMG!, you did not set the input field name in your form. – Al Amin Chayan Aug 01 '15 at 06:08
  • please update your revise code in your question. it will be better to get the error. – Al Amin Chayan Aug 01 '15 at 06:20
  • what is the extension of your php file? i.e. .php or .html? – Al Amin Chayan Aug 01 '15 at 06:24
  • Please see this [http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page] post. This might help you. – Al Amin Chayan Aug 01 '15 at 06:41
  • ok I added to my html code: include_once("send_form_email.php"); ?> – Arie Aug 01 '15 at 06:51
  • I added to html to include .php file an now its working. but after I sent email and I saw message: Thank you for contacting us. We will be in touch with you very soon. " I didn't get any email on my mailbox. Why it can be? – Arie Aug 01 '15 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84864/discussion-between-chayan-and-stackuser). – Al Amin Chayan Aug 01 '15 at 06:52
  • ' I didn't get any email on my mailbox. Why it can be? ', that's what i'm talking about, with your function, the mail doesn't provide a secure source. You need to send it from a reliable source of mail. Read [this article](http://www.html-form-guide.com/email-form/php-script-not-sending-email.html) maybe is useful to solve your doubts and see my andswer please. – Carlos Delgado Aug 01 '15 at 08:23
1

I seriously Don't recommend use php mail function, because sometimes in my case don't work ... Additionally you don't set the method in you form , you have to set it like :

<form method="post" action="ha.php>

And too , you're sending the form with fields that php cannot get ! for example :

<input type="text" class="form-control" id="last_name" placeholder="Enter you last name"> <!-- Where is the name="last_name" ? Otherwise you can't retrieve it with php -->

There's a php Library that will make your life easy about the emails :

SwiftMailer

There's a lot of useful documentation that you could read in the official docs of SwiftMailer

For example to send a Mail you could try with

  require_once 'lib/swift_required.php';

 // Create the Transport
  $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) // the smtp of google or live.
//google for example neeeds : Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
    ->setUsername('your username')
    ->setPassword('your password')
  ;
 //live for example

 $transport = \Swift_SmtpTransport::newInstance()
        ->setUsername('blablaba@outlook.com')->setPassword('blablablabla!')
        ->setHost('smtp-mail.outlook.com')
        ->setPort(587)->setEncryption('tls');

  /*
 You could alternatively use a different transport such as Sendmail or Mail:

 // Sendmail
 $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

 // Mail
 $transport = Swift_MailTransport::newInstance();
 */

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
 ->setBody('Here is the message itself')
;

// Send the message
$result = $mailer->send($message);
Carlos Delgado
  • 2,930
  • 4
  • 23
  • 49