-1

HTML:

<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
  <div class="row">
    <div class="col-sm-6 cols">
      <input type="text" name="name" required="required" placeholder="Name*">
    </div>
    <div class="col-sm-6 cols">
      <input type="email" name="email" required="required" placeholder="Email*">
    </div>
    <div class="col-sm-6 cols">
      <input type="text" name="subject" required="required" placeholder="Subject*">
    </div>
    <div class="col-sm-12 cols">
      <textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
    </div>
    <div class="col-sm-12 cols">
      <input type="submit" name="submit" value="Send Message" class="btn btn-send">
    </div>
  </div>
</form>

php/send-contact.php:

<?php
$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message'])); 
$email_from = $email;
$email_to = 'hello@domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $body, 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">

  <head>
    <script>
      alert("Thank you for getting in touch. We will contact you as soon as possible.");

    </script>
    <meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
  </head>

The HTML alert activates and refreshes as it should, but it doesnt send any email...

I have tried numerous email address recipients.

Also, are there any special measures I should take into account (regarding the PHP elements) when adding Google ReCaptcha to this form?

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • What do you get if you put this `print "
    ";print_r($_POST); print "
    ";` at the top of 'php/send-contact'?
    – blackandorangecat Jul 04 '16 at 17:58
  • Hi - I get this, which includes all the info I put into the form: Array ( [name] => test [email] => test@test.test [subject] => This is a test [message] => this is a test [submit] => Send Message ) – captain_noob Jul 04 '16 at 19:00
  • And name and subject are blank? – blackandorangecat Jul 04 '16 at 19:07
  • No, name is: test and subject is: This is a test. But still nothing comes thru to my email address(es) – captain_noob Jul 04 '16 at 19:08
  • Oh I understand. I think you have the wrong (orders) for your parameters in you mailing function. Not that it will fix the issue, but it should be $to, $subject, $body. How come you're sending the emails to "hello@domain.co.uk" rather than the email entered into the form? – blackandorangecat Jul 04 '16 at 19:12
  • @blackandorangecat The contact form is contacting the owner of the domain. So the email entered is meta information for the recipient to respond to. – Jonathan Jul 04 '16 at 19:23
  • using `@` in php is the coding equivalent of stuffing your fingers in your ears and going "lalalalalala can't hear you". Don't use it, ESPECIALLY when your code isn't working. suppressing any possible error messages while trying to figure out why something isn't working is basically just plain dumb. – Marc B Jul 04 '16 at 19:42

1 Answers1

1

So I have tested this, and I think it is doing what you want.

$name = htmlentities($_POST['name']);
$email_from = htmlentities($_POST['email']);  
$subject = htmlentities($_POST['subject']); 
$message = htmlentities($_POST['message']); 

$email_to = 'Admin@Domain.com';//replace with your email

$headers = "From: webmaster@example.com" . "\r\n" . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request.


//Build the body of the eamil
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email_from . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'message: ' . $message;

$success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers);
?>
<!DOCTYPE HTML>
<html lang="en-US">

  <head>
  <?php if($success){  //If the email was sent correctly?> 
    <script>

      alert("Thank you for getting in touch. We will contact you as soon as possible.");

    </script>

    <?php header('Location: ../index.html'); }else{?>
        <script>

            alert("There was an error when sending the email, please try again later.");

    </script>

<?php header('Location: ../index.html');  } //If the email falied?>
  </head>

The other file (the html) remains the same. Simply replace your code in php/send-contact.php with this.

blackandorangecat
  • 1,246
  • 5
  • 18
  • 37
  • Thank you very much for this! I very much appreciate it! Sadly, I am still getting "There was an error when sending the email, please try again later." and then am left on 'php/send-contact.php' which is displaying "header('Location: /index.html');"... Am I missing something? – captain_noob Jul 04 '16 at 20:04
  • @captain_noob I made a small edit, with the header function. the location and the path. – blackandorangecat Jul 04 '16 at 20:08
  • I'm Not sure why you'd still be getting an error.. I tested this code on my own server and it worked fine. Make sure you read through some of the answers on the linked duplicate question – blackandorangecat Jul 04 '16 at 20:09
  • will do - thanks again – captain_noob Jul 04 '16 at 20:34