0

I want my contact form to send me an email once filled out and the user hits submit. So far, the page will take me to the "Your message has been sent!" page (confirmation.htm), yet I never receive an email. I place my email in the section that says

$to = "myEmail@mail.com";

I've checked my spam folder as well, it doesn't go there either.

Here's my contact form (HTML):

<div class="span">

<form action="mailer.php" method="post" class="comments-form contact-form">
<input type="text" name="name" placeholder="Your Name*" class="name" />
<input type="text" name="email" class="email" placeholder="Your Email*" /> 
<select>
<option value="Subject">Subject</option>
<option value="content-writing">Content Writing</option>
<option value="other">Other</option>
</select>
<textarea class="message" type="text" name="comment" placeholder="Your Message*"></textarea>
<input name="submit" type="submit" class="submit-comment" value="Send Message" />
</form>
</div>

Then here's my php:

<?php
if(isset($_POST['submit'])) {
$to = "myEmail@mail.com";
$subject = "Message from your Portfolio!";

// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";

// sending the message
mail($to, $subject, $body);

// redirect to confirmation
header('Location: /confirmation.htm');
} else {
echo "Something went wrong :(";
}
?>
  • 1
    if you have access to the server mail log file, that is where i would start –  Sep 19 '16 at 00:19
  • 1
    Should `$body` be `$body = " From:".$name_field."\n\n E-Mail:".$email_field."\n\n Message:\n\n ."$comment;`? – guest271314 Sep 19 '16 at 00:23
  • Thanks nogad, but unfortunately that didn't help. It just gave me an error page rather than going to confirmation.htm –  Sep 19 '16 at 00:30
  • First thing I always do is check to make sure PHP is actually sending email, e.g.; put ` – timgavin Sep 19 '16 at 00:33
  • it's also always going to redirect because you're not checking to see if the mail actually sent, you're just skipping right past it and redirecting. – timgavin Sep 19 '16 at 00:38
  • Thank you timgavin, testing this takes me to a blank page (the php file) but does not send me an email.. –  Sep 19 '16 at 00:39
  • 1
    @Danny You may not be able to send mail via PHP's `mail()` function; some hosts don't allow this - such as RackSpace. You may need to use SMTP. Try [PHPMailer](https://github.com/PHPMailer/PHPMailer). or [Swift Mailer](http://swiftmailer.org/) – timgavin Sep 19 '16 at 00:45
  • @Danny Also, check to see if there's an `error_log` file in the same directory as your script. This should tell you what's happening. – timgavin Sep 19 '16 at 00:46
  • No error log.. Checking into your other two links now. Thanks! –  Sep 19 '16 at 00:50
  • Who is your hosting provider? – Ben Shoval Sep 19 '16 at 00:54
  • Try sending mail to another address too, like a Gmail account. – DavidG Sep 19 '16 at 00:56
  • I'm using byethost for the website. Gmail for my email.. –  Sep 19 '16 at 01:02

1 Answers1

-1

This isn't an answer to your problem, but I wanted to show you why your script is always redirecting, even if it isn't sending email.

You need to check to make sure the mail was actually sent. The PHP mail() function will return a boolean value (true or false) depending on if the mail was sent or not, so...

if(mail($to, $subject, $body)) {
    // yay! mail() returned TRUE; mail was sent!
    header('Location: /confirmation.htm');
} else {
    // boo! mail() returned FALSE; mail was not sent :(
    echo "Something went wrong :(";
}

Hope this helps. :)

timgavin
  • 4,972
  • 4
  • 36
  • 48
  • Awesome, thank you! Added to my script :) –  Sep 19 '16 at 01:07
  • his check was perfectly valid for seeing if the form submitted `if(isset($_POST['submit'])) {` also mail's return value says noting about if an email is sent, all it tells you is if the local MTA accepted the mail –  Sep 19 '16 at 01:32
  • @nogad I never mentioned his checking if the form submitted, i was only referring to his checking if the mail was sent. Your down vote is completely unwarranted. – timgavin Sep 19 '16 at 14:03