0

I'm trying to setup a contact form on a website I'm working on, the code for which looks like this:

<form action="contact.php" method="POST">
           <ul>
        <li>
            <label for="name">Name:</label><br>
            <input type="text" name="name" />
            </li>
            <br>
            <li>
            <label for="email">E-mail:</label><br>
            <input type="email" name="email" />
            </li>
            <br>
            <li>
            <div id="label">Enquiry:</div>
            <select name="interest"><br>
            <option value="Wedding">Wedding</option>
            <option value="Engagement/Couple">Engagement or Couple</option>
            <option value="Portrait">Portrait</option>
            <option value="Family">Family</option>
            <option value="Children">Children</option>
            </select>
            </li>
            <br> 
            <li>
            <label for="message">Message:</label><br>
            <textarea name="message" cols="40" rows="6"></textarea>
            </li>
            <br>
            <li>
            <button class="submit" type="submit" value="Submit">Submit Form</button>
            </li>
       </ul>
</form>

Originally I just had the form action as a mailto:myemail@example.com but I would obviously prefer for the user to just submit the form and the email be sent instead of just opening their email application. I'm trying to build a PHP script to do this but am having no luck as yet, probably because this is my first time ever even looking at PHP, let alone trying to write it.

I have looked at many different examples and delved into the PHP manual for help but whatever I write leads to the same result, pressing the submit button on my form and receiving nothing in my inbox. So I started a sort of fault finding process to try and help determine the problem. I would start with really basic PHP scripts and then try and add more to each one and see where it broke.

I started by changing the form action to the php test script as below:

<?php
phpinfo() ;
?>

This worked fine and displayed the php info page as it should. Great so I know that my host supports php and has it installed. This is not the problem then. Second I changed the form action to this code below:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?> 
</body>
</html>

And again it worked as expected showing the Hello World paragraph. After this I attempted the most basic php mail script I could think of which looked like this:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
mail('myemail@example.com', 'Contact Form', 'This is a test');
?>
</body>
</html>

All this succeeds in doing when I press the submit button on my form is displaying a blank page with the url of my php script in the address bar of my browser and from this I have received no email. Not in my inbox or junk folder, nowhere. As this is such a simple script I have two trains of thought at the min. 1 is that I'm just doing something very basic wrong, which is a high possibility considering my extremely limited knowledge of php. 2 is that something more complicated is going on, maybe php settings of my host are not correct for what I'm attempting to do? I don't know.

I apologise for this question being so long winded, I just wanted to show what I'd done so far in the hope that it will help you guys with any assistance you can give me. Any help with my problem is greatly appreciated, thank you.

Dan
  • 671
  • 2
  • 7
  • 12
  • 1
    1. The page is blank because you made it blank. It has no content at all. 2. `mail()` returns a boolean if it was successful or not. Check to see what it says. – John Conde Nov 12 '13 at 21:42
  • 1
    Do you have a mail server installed? – Paul Dessert Nov 12 '13 at 21:44
  • All points above are correct. I tested your (mail) code and it did work. It ended up in my SPAM box due to no headers, but it did nonetheless make it over. I assume you're trying to run this via `localhost`? Check your `.ini` files. – Funk Forty Niner Nov 12 '13 at 21:52
  • My host has blocked mail server for the package I'm on. Something I should've checked but in my frustration I've overlooked it. Thanks for not answering my stupid question with stupid answers guys. Sorry to have made you read all that for nothing really but a fresh head and pair of eyes always works wonders!! – Dan Nov 12 '13 at 22:52
  • Indeed, having extra pairs of eyes does help. Just glad you were able to figure out the problem and possibly get a working service going, cheers @Dan – Funk Forty Niner Nov 13 '13 at 00:20
  • Cheers Fred. Obviously my actual script will include headers and the like, was just building up in stages to find the break in the chain. I'm still a long way from finished yet, especially with my lack of knowledge, but this is another hurdle overcome! Thanks for lending your eyes – Dan Nov 13 '13 at 00:28

4 Answers4

1

One thing to consider is that your email may be getting blocked by your email provider. I have a host with Heart Internet and I've tried sending an email from the website being hosted by Heart to my AoL account and the email does not show up at all. But any emails going to Gmail do. This is because AoL has blocked all emails coming from Heart because of websites producing spam.

I will input some PHP code below which should work:

// Input your desired location instead of email address
$to = 'email address';
$subject = 'Test email';

$message = '<html><body>';
    $message .= '<h1>This is an email.</h1>';
    $message .= '<p>I hope you like it.</p>';
$message .= '</body></html>';

$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';

mail($to,$subject,$message,$headers);
harvzor
  • 2,832
  • 1
  • 22
  • 40
  • 1
    Thanks for this info, I'll give this a try when I get my mail server issues sorted. – Dan Nov 12 '13 at 22:54
1

Or you are running from localhost.
Or your SMTP settings in your php.ini file aren't configured correctly

vincent kleine
  • 724
  • 1
  • 6
  • 22
0

My host has blocked mail server for the package I'm on. Something I should've checked but in my frustration I've overlooked it. Thanks for not answering my stupid question with stupid answers guys. Sorry to have made you read all that for nothing really but a fresh head and pair of eyes always works wonders!!

Dan
  • 671
  • 2
  • 7
  • 12
-2

check at the top of the form, there is written <form action="contact.php" method="POST"> that's mean form values are posted to contact.php file where the mail functions is written to send email with form's data. Please show the code of contact.php

StreetCoder
  • 9,871
  • 9
  • 44
  • 62
  • The OP is changing the code in `contact.php`. The last example is where it's failing – Paul Dessert Nov 12 '13 at 21:49
  • are you trying from localhost? if so please upload to the hosting server. – StreetCoder Nov 12 '13 at 21:51
  • Thnx for your comments guys. Yeah I was changing the code of the contact.php in the steps of the question. It was uploaded to my hosting server and that's my problem because it has mail server disabled for my package. Thanks for your input and taking the time to help. – Dan Nov 12 '13 at 22:56
  • my answer helps you but I got - vote :( – StreetCoder Nov 13 '13 at 02:33