574
<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: yoursite.com'; $to = '[email protected]'; $subject = 'Customer Inquiry'; $body = "From: $name\n E-Mail: $email\n Message:\n $message"; if ($_POST['submit']) { if (mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent!</p>'; } else { echo '<p>Something went wrong, go back and try again!</p>'; } } ?> 

I've tried creating a simple mail form. The form itself is on my index.html page, but it submits to a separate "thank you for your submission" page, thankyou.php, where the above PHP code is embedded. The code submits perfectly, but never sends an email. How can I fix this?

6
  • 3
    is your program in local computer or live host Commented Jul 9, 2014 at 2:20
  • 9
    1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box? Commented Jul 9, 2014 at 2:21
  • its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not? Commented Jul 9, 2014 at 2:22
  • 1
    try send without $from Commented Nov 8, 2017 at 14:31
  • I've run into a similar problem before, and it wasn't caused by any of the errors listed in the recommended answer below. It's because my DNS lists another server as the authorised mail server, but the web server itself is configured to recognise itself as the authorised mail server too. So when PHP mail() sends emails to its own domain, it does not check the DNS records to find the correct mail server, and just forwards the email to itself. I've wrote an article about it detailing the fix: blog.terresquall.com/2021/04/… Commented Jan 15, 2022 at 16:09

31 Answers 31

1
2
-1

It may be a problem with "From:" $email address in this part of the $headers:

From: \"$name\" <$email> 

To try it out, send an email without the headers part, like:

mail('[email protected]', $subject, $message); 

If that is a case, try using an email account that is already created at the system you are trying to send mail from.

Sign up to request clarification or add additional context in comments.

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.