Adam G
Adam G

Reputation: 357

php email form issue

I am having trouble setting up a PHP form. Its my first try at doing this. I have got the form to submit and send to my email. But i receive 2 emails.

enter image description here

Is it ment to say (unknown sender) ???? surely not.

enter image description here

and in this image it shows you that after only one form submit. it send one blank email, and one with text. But only the message. Whats wrong?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "[email protected]";
$subject = "New Message";

mail ($to, $subject, $message, "From: " . $name);
echo "Your Message has been sent";

?>

      <form id="contactform" name="contact" method="post" action="#">
  <div class="row">
    <label for="name">Your Name<span class="req">*</span></label>
    <input type="text" name="name" id="name" class="text" tabindex="1" placeholder="enter name" required>
  </div>
  <br>
  <div class="row">
    <label for="email">E-mail Address<span class="req">*</span></label>
    <input type="email" name="email" id="email" class="text" tabindex="2" placeholder="[email protected]" required>
  </div>
  <br>
  <div class="row">
    <label for="subject">Subject<span class="req">*</span></label>
    <input type="text" name="subject" id="subject" class="text" tabindex="3" placeholder="subject title" required>
  </div>
 <br>
  <div class="row">
    <label for="message">Message<span class="req">*</span></label>
    <textarea name="message" id="message" class="textarea" tabindex="4" required></textarea>
  </div>
  <br>
  <div class="row">
  <input type="submit" name="submit" value="Send Message" id="submit" />
  </div>
  </form>

Upvotes: 0

Views: 155

Answers (1)

MrCode
MrCode

Reputation: 64536

The From header should be an email address or both a name and email address in the format:

From: John Smith <[email protected]>

It should never be just a name, which is what you appear to be doing. Also remember to terminate headers with \r\n.

Using user input as the From header is not usually a good idea because it can introduce a header injection vulnerability, and it can also cause inconsistent spam filter results. For a contact form, I suggest just using a static email address of your own.

Upvotes: 4

Related Questions