0

I'm learning php, and now I'm trying to make simple form that on submit will send e-mail to my mail. I'm using Vamp server on local PC. When i press submit button it just opens the below code from PHP file and displays it on the screen. No other action, can someone explain it to me what is happening and what am I doing wrong i just want to submit and see the mail on my gmail acount so i know it works. Directory consist of: form-page.html, form-to-mail-php and thank_you html. In form-page you have inputs and submits, in php code below and it redirects you to thank you page.

            <?php
    if(!isset($_POST['submit']))
    {
      //This page should not be accessed directly. Need to submit the form.
      echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    //Validate first
    if(empty($name)||empty($visitor_email)) 
    {
        echo "Name and email are mandatory!";
        exit;
    }

    if(IsInjected($visitor_email))
    {
        echo "Bad email value!";
        exit;
    }

    $email_from = 'l.lawliet46@gmail.com';//
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
        "Here is the message:\n $message".

    $to = "l.lawliet46@gmail.com";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');


    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      $injections = array('(\n+)',
                  '(\r+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }



    ?> 



<form method="post" name="myemailform" action="form-to-email.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>
user3187715
  • 137
  • 12
  • is PHP module installed/activated on the server? – RST Apr 12 '15 at 09:33
  • Ye yes it port free, green light everything. I just can't wrap my head around it it should be really simple right? – user3187715 Apr 12 '15 at 09:35
  • Firstly, move the HTML snippet before the php snippet (because of `exit`). Secondly, make sure the file you are making request to (form-to-email.php) exists and is equal to this file. Also, beware of spam filters and greylists/blacklists so configure your mail server properly. – sitilge Apr 12 '15 at 09:43
  • It's in two separate files. As you can see it calls for form-to-email.php on in same directory. File is there, after that it should redirect to thank_you.php. It's just the order i put the code in. And when you say mail server you mean my google acount server? Or is there in php some mail thing i need to configure? – user3187715 Apr 12 '15 at 09:46
  • Can you adjust your question and add what is in which file? – RST Apr 12 '15 at 09:50
  • There i wrote everythig that is in the folder. – user3187715 Apr 12 '15 at 09:53
  • Are other PHP scripts working? Does your script have the correct file name extension? – Oswald Apr 12 '15 at 10:02
  • Other scripts are working perfectly. Anything with .php on it opens as it should, echos, print, calculate all simple task but mail is not working – user3187715 Apr 12 '15 at 10:07

1 Answers1

1

First off, the code being displayed instead of an action performed by the server is indicative of a missing PHP module. Try running a simple phpinfo() script in a new file and look at what is returned. For instance,

<?php
phpinfo(INFO_MODULES);
?>

will output modules installed on your machine. With WAMP an even simpler option exists for modifying your php.ini file. See below,

Edit php.ini file

You should note that .html files with PHP script inside them need the PHP handler to be modified for PHP scripts. See below,

Wamp Server isn't executing php code

You could use $.ajax to pass the form values from form-page.html to form-to-mail.php to circumvent modifying the PHP handler file in the previous step.

Next, Consider these:

This is pretty basic tutorial on how to configure WAMP to send mail.

http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html

Also, your Google account (I think) will not allow you to send mail without authentication. Please correct me if that is not the case.

How to configure WAMP (localhost) to send email using Gmail?

In Closing, it's a good practice to use mysqli_real_escape_string() on your inputs.

Community
  • 1
  • 1
bowl0stu
  • 352
  • 1
  • 12