Search code examples
phphtmlformsemailmailer

PHP mailer to send simple contact form


I've been trying to use the mail() function with PHP but can't seem to get a message to send to my email address when I test the contact form. It simply goes to a white screen and no further.. It doesn't even show the success message underneath.

This is the PHP:

<?php 
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$number = $_POST['number'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = 'Message from Reef website';
$to = 'myemail@hotmail.co.uk';

if (empty($name) || empty($address) || empty($number) || empty($email) || empty($subject) || empty($message)) {
    if (empty($name))
        $error['name'] = "Please enter your Full Name";
    if (empty($name))
        $error['address'] = "Please enter your address";
    if (empty($name))
        $error['number'] = "Please enter a contact number";
    if (empty($email))
        $error['email'] = "Please enter a valid Email Address";
    if (empty($message))
        $error['message'] = "Please write a message, enquiries or other concerns above";
}
else { //if not empty

    $headers="From: {$email}\r\nReply-To: {$email}";
    mail($to,$subject,$message,$headers);
    $success = "Thank you! You're email has been sent.";
}
}
?>

This is my HTML form:

<form method="POST" action="mailer.php">
    <table border="1">
            <tr>
    <td><label for="name" class="g">Name</label></td>
    <td><input id="name" name="name" type="text" required autofocus></td>
    </tr>

    <tr>
    <td><label for="address" class="g">Address</label></td>
    <td><textarea rows="3" id="address" name="address" cols="50"></textarea></td>
    </tr>

    <tr>
    <td><label for="number" class="g">Contact number</label></td>
    <td><input id="number" name="text" type="text" required autofocus></td>
    </tr>

     <tr>
      <td><label for="email" class="g">Email</label></td>
      <td><input id="email" name="email" type="text" placeholder="example@domain.com" required autofocus></td>
      </tr>

      <tr>
      <td><label for="message" class="g">Enquiry</label></td>
      <td><textarea rows="3" id="message" name="message" cols="50"></textarea></td>
      </tr>
      </table>
      <input type="submit" value="Submit" name="submit">
</form>

Can anyone see what's missing? Looking at other similar questions, there's are all like mine but they say that they work.


Solution

  • The reason it's going to a blank page is that the PHP page is blank! You don't seem to be echoing your success message anywhere on the page.

    I would recommend changing your form so that the HTML is as below:

    <form method="POST">
    

    And then paste the PHP into the top of that page. Remember to set the page type to .php rather than .html/.htm

    Finally add an echo to the top of your form so the success message will display, along the lines of

    <?php
        if ($success) {
            echo $success;
        }
    ?>