0

I want to create a contact form using PHP mailing function. I am learning the process by developing on a free web hosting provider. Here are my files:

index.html

<!DOCTYPE html>
<html>  
    <head>
        <meta charset="utf-8">
        <title>HTML5 Contact Form</title>
        <link rel="stylesheet" media="screen" href="styles.css">
    </head>     
    <body>
        <div id="contact">
            <form class="contact_form" action="contact.php" method="post" name="contact_form">
                <ul>
                    <li>
                         <h2>Contact Us</h2>
                         <span class="required_notification">* Denotes Required Field</span>

                    </li>
                    <li>
                        <label for="name">Name:</label>
                        <input type="text" id="name" name="name" placeholder="John Doe" required />
                    </li>
                    <li>
                        <label for="email">Email:</label>
                        <input type="email" name="email" id="email" placeholder="john_doe@example.com" required /> <span class="form_hint">Proper format "name@something.com"</span>

                    </li>
                    <li>
                        <label for="message">Message:</label>
                        <textarea name="message" id="message" cols="40" rows="6" required></textarea>
                    </li>
                    <li>
                        <button class="submit" id="submit_btn" type="submit">Submit Form</button>
                    </li>
                </ul>
            </form>
        </div>
    </body>
</html>

contact.php

<?php
    $field_name = $_POST['name'];
    $field_email = $_POST['email'];
    $field_message = $_POST['message'];
    $mail_to = 'babloopuneeth@gmail.com';
    $subject = 'Message from a site visitor '.$field_name;
    $body_message = 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Message: '.$field_message;
    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";
    $mail_status = mail($mail_to, $subject, $body_message, $headers);

    if ($mail_status) {
        ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'sample.html';
        </script>
    <?php
     } else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to gordon@template-help.com');
            window.location = 'sample.html';
        </script>
    <?php
     } 
?>

I also have a styles.css file. I'm getting the alert box saying Thank you for the message. But I'm not getting a mail.

Can anyone help me? Where am I going wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Puneeth P
  • 155
  • 4
  • 18

3 Answers3

2

The free hosting service you're using may not allow this functionality (I would of thought it wouldn't) have you checked?

Also check out this post:

Community
  • 1
  • 1
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
1

Please check your hosting service, I am sure your code is all good as I actually tested it and it worked perfectly. :) You may want to test it yourself on http://goo.gl/sDIL6. (I will keep it there for a few days..)

Fill out the form


Received message

Morris Miao
  • 720
  • 1
  • 8
  • 19
0

Does your hosting service block port 25? I have tested it on my server and it works perfectly.

If you are using 000webhost then it does. That is the most common free service and they removed support for mail a while back.

Dummy Code
  • 1,858
  • 4
  • 19
  • 38