0

Please note that this question has been asked by several others and none of them seem to have a solution that worked for me. I just finished trying Roy's idea from his answer here without any success and hence posting yet another question. I have my contact form built using Bootstrap and I have a PHP file to field the email submission. Here's the form code:

<form class="form-horizontal" role="form" name="contact-form" id="contact-form" action="contact.php" method="post">
              <div class="modal-header">
                <h4>Let‘s get talking!</h4>
              </div>
              <div class="modal-body">
                <div class="form-group">
                  <label for="contact-name" class="col-xs-2 control-label">Name</label>
                  <div class="col-xs-10">
                    <input type="text" class="form-control" name="contact-name" id="contact-name" placeholder="John Doe">
                  </div>
                </div>
                <div class="form-group">
                  <label for="contact-email" class="col-xs-2 control-label">Email</label>
                  <div class="col-xs-10">
                    <input type="email" class="form-control" name="contact-email" id="contact-email" placeholder="example@domain.com">
                  </div>
                </div>
                <div class="form-group">
                  <label for="contact-message" class="col-xs-2 control-label">Message</label>
                  <div class="col-xs-10">
                    <textarea class="form-control" name="contact-message" rows="5" placeholder="Your message"></textarea>
                  </div>
                </div>
              </div>
              <div class="modal-footer">
                <a class="btn btn-default btn-lg" data-dismiss="modal">Close</a>
                <button type="submit" name="submit" id="submit" class="btn btn-primary btn-lg">Send</button>
              </div>

And here's the script within contact.php:

<?php

if (empty($_POST)) {
    print "<p>No data was submitted.</p>";
    print "</body></html>";
    exit();
}


$name = $_POST["contact-name"];
$email = $_POST["contact-email"];
$message = $_POST["contact-message"];

$EmailTo = "amitsc@icloud.com";
$Subject = "New Message Received";


// send email
$test = mail($EmailTo, $Subject, $message, "From: ".$name." <".$email.">");
if ($test) {echo "successfuly sent";} else {echo "sending failed";}

?>

Every time I hit the Send button in the form, it returns the message successfully sent, however no email actually gets sent out. To make things even funnier, I still get the same message (successfully sent) even when I submit a blank form! And that's despite the if block at the beginning of contact.php that is supposed to say No data was submitted in such a scenario. What am I doing wrong?

Update: I just did a var_dump($_POST); and here's the output on submitting a blank form:

array(4) { ["contact-name"]=> string(0) "" ["contact-email"]=> string(0) "" ["contact-message"]=> string(0) "" ["submit"]=> string(0) "" } successfuly sent

P.S.: For what it's worth, these files are currently sitting on my computer running a MAMP stack. It's not hosted on a public server yet because I'm still building it out. Could this have any bearing on the goofy results?

Community
  • 1
  • 1
TheLearner
  • 2,813
  • 5
  • 46
  • 94
  • which email server do you use ? Are you in your local computer ? – MD. Atiqur Rahman Sep 06 '15 at 10:46
  • `$_POST` will _never_ be "empty" if you submit a form. At least the submit button will be in there. But also all the input fields, only _they_ contain an empty value. Check yourself by dumping `$_POST`. – arkascha Sep 06 '15 at 10:48
  • @MD.AtiqurRahman: Like I mentioned toward the end of my question (P.S.), everything is currently on my local computer. – TheLearner Sep 06 '15 at 10:51
  • @FerozAkbar: Nothing in the Spam folder either. – TheLearner Sep 06 '15 at 10:51
  • Hey man ! While trying sending email from local computer you need to install a emailer package . Why don't you try it in your web server ? It will work fine there. – MD. Atiqur Rahman Sep 06 '15 at 10:53
  • @MD.AtiqurRahman You mean running it on my local computer running Apache and PHP is not enough? I thought MAMP was a web server, no? – TheLearner Sep 06 '15 at 10:57
  • @arkascha: Just did a var_dump and updated the question with its output. – TheLearner Sep 06 '15 at 10:57
  • Hey , check out my answer bellow – MD. Atiqur Rahman Sep 06 '15 at 10:58
  • Sure, just as I said. So now you have your answer. – arkascha Sep 06 '15 at 10:58
  • @arkascha: I don't understand. If var_dump shows 0-length strings for each variable as output, doesn't it mean there's nothing in $_POST? – TheLearner Sep 06 '15 at 11:01
  • 1
    Well, as you clearly proved, there are 4 elements in `$_POST`. So it is not empty, is it? – arkascha Sep 06 '15 at 11:02
  • Ahh, I see...so so it doesn't matter if the elements are themselves empty, right? I get it now. Sorry for the stupidity. – TheLearner Sep 06 '15 at 11:04
  • @arkascha: If the reason my mail isn't going out is the absence of an email server, shouldn't my php script result in an error? It says `successfully sent` invariably. – TheLearner Sep 06 '15 at 11:14
  • Well, who says that the call to `mail()` succeeds? You never check that! Did you read the documentation of that function? It clearly states that the return value is a boolean indicating if the call succeeded or not: http://php.net/manual/en/function.mail.php So you want something like this: `$mailSuccessfullySent = mail(.....); if ($mailSuccessfullySent) { echo "successfuly sent"; } else { echo "an error occurred"; }` – arkascha Sep 06 '15 at 11:23
  • Also you really should start looking into your http servers error log file. That is where php errors are usually logged to. No sense in trying to _guess_ what the issue might be if you can look into that file and simply _read_ what the error actually _is_. – arkascha Sep 06 '15 at 11:27
  • @arkascha I have the mail() function wrapped in an if-else to test for success and it does return true, i.e. "successfully sent." The test code is now updated in the question. – TheLearner Sep 06 '15 at 12:09
  • If the `mail()` command returns `true`, then the message has been dispatched. Question is where to and what happened afterwards. That obviously depends on your configuration and the systems involved which is something we don't know anything about. You will have to share your configuration. – arkascha Sep 06 '15 at 12:41
  • This is one of the most common question on Stack Overflow. When you see it asked please vote to close it as a duplicate. – John Conde Sep 06 '15 at 13:57

0 Answers0