Hi all!

I'm putting a contact form together to receive emails. It is giving the appearance of sending the form data through to my email address (i.e the "thank you" message appears) but nothing is coming through.
I'm not sure if I have incorrect html in my form or if it is a problem with the hosts mail server. No mail server errors are showing though.
I'm wondering if I've given the wrong label values for "for"?
I felt it was better to post this here rather than an html forum.

My script is as below:

/* michael_dev_contact.php */

<h1>Contact Me</h1>
        <?php 

        // Set the default timezone:
        date_default_timezone_set('Europe/London');

        // Check for form submission:
        if(isset($_POST['submitted']))
        {
            // Minimal form validation:
            if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']))
            {
                // Create the body:
                $body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";

                // Make it no longer than 70 characters long:
                $body = wordwrap($body, 70);

                // Send the email:
                mail('myemail@email.com', 'Contact Form Submission', $body, "From: {$_POST['email']}");

                // Print a message:
                echo '<p><em>Thank you for contacting me at '.date('g:i a(T)').' on '.date('l F j, Y').
                        '. I will reply as soon as possible.</em></p>';

                // How long did it all take:
                /*echo '<p><strong>It took '.(time() - $_POST['start']).' seconds for you to complete
                    and submit the form.</strong></p>';
                */

                // Clear $_POST (so that the form's non-sticky):
                $_POST = array();
            }
            else
            {
                echo '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';
            }
        } // End of main isset() IF.

        // Create the html form:
        ?>
        <p>Please fill out this form to contact me.</p>
        <form action="michael_dev_contact.php" method="post" class="contact">
        <div>
            <label for="name" class="fixedwidth">Name</label>
            <input type="text" name="name" size="30" maxlength="60" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" class="txt"/>
        </div>
        <div>
            <label for="email" class="fixedwidth">Email Address</label>
            <input type="text" name="email" size="30" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" class="txt"/>
        </div>
        <div>
            <label for="comments" class="fixedwidth">Comments</label>
            <textarea name="comments" rows="5" cols="30" class="txt"> <?php if(isset($_POST['comments'])) echo $_POST['comments']; ?> </textarea>
        </div>
        <div class="button">
            <input type="submit" name="submit" value="Send!" />
        </div>
            <input type="hidden" name="start" value="<?php echo time(); ?>" />
            <input type="hidden" name="submitted" value="TRUE" />
        </form>

Thanks in advance for any help offered.

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

@Tinnin

I'm not sure if I have incorrect html in my form or if it is a problem with the hosts mail server. No mail server errors are showing though.

I think you can take this class="contact" off in your <form>

From this:

<form action="michael_dev_contact.php" method="post" class="contact">

to this:

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

You also don't need this <div class="button"> and also <div>:

From this:

<div class="button">
<input type="submit" name="submit" value="Send!" />
</div>

to this:

<input type="submit" name="submit" value="Send" />

So you form should look like this:

<style>
fieldset{width:225px;margin-left:15px;border:0px solid #ffffff;padding:4px;}
</style>
<form action="michael_dev_contact.php" method="post">
<fieldset> 
<label for="name" class="fixedwidth">Name</label>
<br>
<input type="text" name="name">
<br>
<label for="email" class="fixedwidth">Email Address</label>
<br>
<input type="text" name="email">
<br>
<label for="comments" class="fixedwidth">Comments</label> 
<textarea name="comments" rows="5" cols="30"></textarea>
<br>
<input type="hidden" name="start" value="<?php echo time(); ?>" />
<input type="hidden" name="submitted" value="TRUE" /><br>
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>

But the classes are just for CSS. They wouldn't have an impact on the functionality.

Fixed it. I didn't realise I had to send it my hosting mail address. Live and learn. Thanks for the input though LastMitch. Always appreciated.

Your code seems to be allright. There are two things that come to my mind.

First one is to check whether the first block of the if executes at all since maybe you are doing too little of checking of the $_POST values - namely you are not checking for the existence of elements (using isset). I would do it this way:

if(isset($_POST['name']) && 
   !empty($_POST['name']) && 
   isset($_POST['email']) && 
   !empty($_POST['email']) && 
   isset($_POST['comments']) && 
   !empty($_POST['comments'])

You can also test this with placing this code on line 15:

die('Howdy');

and see if it shows up.

The second thing to check is whether your mail server works. You can try that by sending a simple test mail in a debug script which would be something like:

<?php
    if(mail(youremail@email.com , 'Mail test' , 'Testing mail')) {
        echo 'Looks like it is  working';
    } else {
        echo 'Something has gone wrong';
    }
?>

Thanks broj1. Good suggestions. I'll bear them in mind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.