0

I have a PHP mail script set up and keep hitting the error "Something went wrong, go back and try again!" all the form fields have been checked and all the names match etc so I am wondering if there is something wrong with my script?

<form method="post" action="contact.php" id="contactForm">
                            <label for="name">Name</label>
                            <input type="text" id="name" name="name" class="name" />
                            <label for="email">Email</label>
                            <input type="text" id="email" name="email" class="email" />
                            <label for="phone">Phone</label>
                            <input type="text" id="phone" name="phone" class="phone" />
                            <label for="iam">I Am</label>
                            <select name="iam" class="iam" id="iam">
                                <option>a recruiter looking to recruit staff</option>
                                <option>a construction worker looking for work</option>
                                <option>requesting information</option>
                            </select>
                            <label for="message">Message</label>
                            <textarea name="message" id="message" class="message"></textarea>

                            <label for="captcha">What is 3+4?</label>
                            <input type="text" id="captcha" name="captcha" class="captcha" />
                            <input type="submit" name="submit" id="submit" class="submit" />
                        </form>




<?php
$name    = $_POST['name'];
$phone   = $_POST['phone'];
$email   = $_POST['email'];
$iam    = $_POST['iam'];
$human   = $_POST['captcha'];
$message = $_POST['message'];
$from    = 'From: Test';
$to      = 'sales@test.com';
$headers = "From: $email";
$subject = 'Tradeline Contact';
$body    = "From: $name\n E-Mail: $email\n Phone Number:\n $phone I Am:\n $iam Message:\n $message";

if ($_POST['submit'] && $human == '7') {
    if (mail($to, $subject, $body, $headers, "-f " . $from)) {
        echo '<p>Your message has been sent!</p>';
        header( 'Location: http://urlhere.com/thankyou.html' ) ;
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
} else if ($_POST['submit'] && $human != '7') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>

Any help is appreciated.

Harry Berry
  • 65
  • 10
  • *"I am wondering if there is something with my script?"* http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Aug 19 '15 at 21:51
  • tip: you're outputting before header – Funk Forty Niner Aug 19 '15 at 21:52
  • Removed echo before header and I have also updated my question to "I am wondering if there is something wrong with my script?" – Harry Berry Aug 19 '15 at 21:56
  • use error reporting as I said above Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Aug 19 '15 at 21:56
  • Tried with error reporting and I am not receiving an error. I feel the script is relatively OK but it is hitting the if else when it shouldn't as it should be submitting and then relocating to the above header location. Any ideas what could be up? – Harry Berry Aug 19 '15 at 21:59
  • looks like mail is returning false, first check it is then debug that –  Aug 19 '15 at 22:07
  • It's formatted fine - what mail host are you using? Also, you might try it with fewer parameters to see if one of them is causing the issue (ie, without setting the envelope sender (take of the -f portion); without setting $headers). – Andy Hoffner Aug 19 '15 at 22:29

1 Answers1

1

You might want to change:

mail($to, $subject, $body, $headers, "-f " . $from)

to:

mail($to, $subject, $body, $headers."\r\n")

That way your mail headers will be in compliance.

Also, turn on error reporting. I happen to use error_reporting(7); right under the <?php line to turn on all common errors with the exception of catching undefined variables, and that will tell me if the mail function has problems.

Another thing you can do is check the mail server logs to see if mail is actually being sent.

I'm sure you already did this, but in case you haven't, make sure you use valid email addresses.

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
  • Really appreciate the help and after checking there was no problems with the code at all. It was server related and seemed to be a common problem with the host I use(1and1). – Harry Berry Aug 20 '15 at 15:01