0

Would this work, i want to email the contents of my form to email@example.com, but when I run it in localhost it doesn't send, I am wondering if the results would be different online, I don't know why it wouldnt work online. I just want the name and message to be send in a email to the specified email address. When I send it there is no errors, and it does redirect me to the page that states thank you for contacting us.

<form name="contactform" method="post" action="send_form_email.php">
<input name="name" placeholder="Your Name"></input>
<br>
<textarea name="message" placeholder="Email Us." style="height:100px;">    </textarea>
<input type="submit" id="sub" value="Submit" placeholder="Submit" style=""></input>
 </form>



<?php
if(isset($_POST['message'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@example.com";
$email_subject = "Customer";
function died($error) {
 // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}
if(!isset($_POST['message'])){
 died('We are sorry, but there appears to be a problem with the form you submitted.');       
}
$name = $_POST['name']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
 $error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$message)) {
 $error_message .= 'The message you entered does not appear to be valid.<br />';
}
 $email_message = "Form details below.\n\n";
 function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
'X-Mailer: PHP/' . phpversion();
 ?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Potatoe
  • 19
  • 3

4 Answers4

0

I don't see any call to the mail function (http://php.net/manual/en/function.mail.php). Did I miss it in your paste, or could that be the issue?

mail($email_to, $email_subject, $email_message);
0

I am wondering if the results would be different online

This depends on if your servers are configured differently, different Apache modules, different versions of PHP, etc.

Without analysing the two environments, it's impossible to say.

when I run it in localhost it doesn't send

I'm guessing, based on the question you are asking, that you haven't setup a local mail server, or setup some SMTP mail settings (such as Gmail).

Search Stack for sending mail on localhost and you can get it working and test before throwing it to the wilds.

Here's a starting example:
php send mail from localhost

Alternatively, on your live server/hosting, password protect the site (or directory if a live site), upload the code to the password protected area so it is not public, and test it yourself!

It does redirect me to the page that states thank you for contacting us.

It doesn't redirect you anywhere, your script simply displays the message "Thank you for contacting us. We will be in touch with you very soon" because it is at the bottom of your script.

So, other than a fatal error or there is no $_POST['message] which would halt the script, any code above that message makes no difference to the message being displayed or not. It will always display that message.

You'd need to put some simple checking in place to identify if the mail was actually sent or not - something like if mail sent echo "ok", else echo "failed".

Community
  • 1
  • 1
James
  • 4,644
  • 5
  • 37
  • 48
0

You need to use mail() to send the data. And before that you need to setup a SMTP server on your localhost or the server you are testing that.

holyknight
  • 315
  • 1
  • 3
  • 13
0

I reviewed the code, added the php mail function, ran it online on my web server and it works just fine. The environment you are testing it on seems to lack a running mail server or it has not been setup properly.

The mail function can be included using this code, the $email_message variable in your code stores the message to be emailed:

<?php
mail("someone@example.com","My subject",$email_message);
?>