2

I am trying to make a contact form with PHP. Shouldnt this code work, or am I overseeing something? It is a little bit basic, but I am just trying to get it work first.

<div>
            <p id="feedback"><?php echo $feedback;?></p>
            <form action="?" method="post">
                <ul>
                    <li>
                        <label for="name">Name:</label>
                        <input type="text" name="name" id="name">
                    </li>
                    <li>
                        <label for="email">Email:</label>
                        <input type="text" name="email" id="email">
                    </li>
                    <li>
                        <label for="message">Message:</label>
                        <textarea id="message" name="message" cols="42" rows="9"></textarea>
                    </li>
                    <li>
                        <input type="submit" value="Submit">
                    </li>
                </ul>
            </form>
        </div>

<?php

$to = 'xxxx@gmail.com';
$subject = 'This came from the other world';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$header = '$email';

if($_POST) {
    mail($to, $subject, $message, $header);
    $feedback = 'Your Message has been send';
}
?>
KrMa
  • 687
  • 2
  • 10
  • 22

3 Answers3

2

Change this

<input type="submit" value="Submit" name="submit"> # add name field

In Your mail code

if (isset($_POST['submit'])) {
    $to = 'xxxx@gmail.com';
    $subject = 'This came from the other world';

    $name = $_POST['name'];
    $email = $_POST['email'];
    $msg = $_POST['message'];

    $message = "Customer Name : ".$name\r\n;
    $message .= "Customer Email : ".$email\r\n;
    $message .= "Customer Message : ".$msg\r\n;

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

    if (!mail($to, $subject, $message, $headers)) {
        echo "Sending Failed";
    }
    else{
        echo "Your Message has been send";
    }
}

If you sending mail in localhost take look at this

  1. XAMPP
  2. WAMP
Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Thanks a lot for your comment. When I try to change my code I get an server error 500. I am actually using MAPP so I have to look at this to. – KrMa Jan 23 '16 at 09:42
  • @MadsKristensen host the project and check. then you can check – Abdulla Nilam Jan 23 '16 at 09:46
  • Ok i will do that. I do not have a domain yet, so I will wait with this function. I am only running it on localhost. Thanks again :) – KrMa Jan 23 '16 at 09:48
1

Your header needs to include actual email headers, you are just repeating the email adddress.

Example:

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

The documentation on the mail() function will tell you why the headers and line breaks are important and give you security tips.

HenryTK
  • 1,287
  • 8
  • 11
  • Thank you a lot for your answer. I just tried to change the code, but I still dosent recieve any mail. Do you think it is because I use my Localhost? – KrMa Jan 23 '16 at 09:47
  • This stackoverflow post covers that in detail: http://stackoverflow.com/questions/18288007/php-send-mail-from-localhost – HenryTK Jan 23 '16 at 10:14
1

try to read this http://php.net/manual/de/function.mail.php

i think your problem is this code line

$header = '$email';

your header will be the string $email and not the value of the $email var

RlDDlCK
  • 606
  • 5
  • 10