Pergunta

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.

Foi útil?

Solução

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;
    }
?>

Outras dicas

Change

mail($to,$subject,$message,$headers); 

to

if(mail($to,$subject,$message,$headers))
{
  echo "mail has been sent";
}
else
{
 echo "error during sending mail";
}

also change your ifs because you check only one variable

if (empty($name))

always $name

put in your first line for debuging:

error_reporting(E_ALL);

why do you use "id" is it necessary for you?

<input id="name" name="name" type="text" required autofocus>
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";

You are using $name in every ifstatement. Change it with corresponding variable.

AND

Change <input id="number" name="text" type="text" required autofocus>

To <input id="number" name="number" type="text" required autofocus>

I think you got it. You are checking empty($number) in parent if statement which is always true, And you are not getting any message because of wrong variable ($name).

What are the SMTP server settings in your php.ini file? You need to have an SMTP server running to send mail.

An easier solution would be to use PHPMailer.

You have one problem

You are trying to access number value like this

$number = $_POST['number'];

but in your form it is name="text"

<input id="number" name="text" type="text" required autofocus>

so change the form field to this

<input id="number" name="number" type="text" required autofocus>

that was causing your validation to fail.

I think, you don't have set SMTP data in php.ini If you can, use some other library to send mail like https://github.com/Synchro/PHPMailer

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top