-1

I've done my research on this but can't seem to get it to work.

I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.

What am I missing? Thank you!

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Temp</title>

</head>
<body>
    <form method="post" action="send.php" name="contact_form">
        <p>
            <input name="name" type="text" />
        </p>
        <p>
            <input name="email" type="text" />
        </p>
        <p>
            <textarea name="message"></textarea>
        </p>
        <p>
            <input type="submit" name="submit" id="submit" value="Submit" />
        </p>
    </form>
</body>
</html>

PHP:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email@myemail.co";
$subject = "Contact Form Submission";

mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
howck
  • 17
  • 3

1 Answers1

1

I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.

Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).

Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname@mydomain.com"). Then, just include the person's name and email address in the $message of the email.

Hope that helps.

Community
  • 1
  • 1
Davis
  • 856
  • 4
  • 11
  • Thanks, Davis. I'll check that out. I, unfortunately, don't know much PHP at all and am kind of working blindly on intuition with this. It would be nice if there was some JS or HTML way to do this. – howck Mar 02 '16 at 22:25
  • For what it's worth, I copied your HTML and PHP directly to my own server and ran it, and the mail arrived as expected, even with only putting `From: John Doe` (it appended a server email address to it). So, I think your code is fine. I'm sure the error lies in your PHP mail configuration or in the sending or receiving email server mishandling or disliking what they're getting. – Davis Mar 02 '16 at 22:46
  • That's very helpful, thanks. I am hosting the code through GoDaddy, and was told by their support that they don't think the issue is on their end. Weird. Either way, thanks. – howck Mar 02 '16 at 22:47