-1

I have a registration form that requires Name, Phone, and and an optional comments section. After running multiple tests, the form seems to submit and (the bottom of the php code) the page redirects me to the "thank you page". But I don't get an email whatsoever. Anyone know what I'm doing wrong?

<form class="register-form" name="registerform" method="post" action="form-to-email.php">

            <div class="form-column">

                <p>Fields marked with * are required.</p>

                <label for="first_name">First Name *</label>
                <input type="text" id="first_name" name="first_name" placeholder="John">

                <label for="last_name">Last Name *</label>
                <input type="text" id="last_name" name="last_name" placeholder="Smith">

                <label for="phone">Phone Number</label>
                <input type="text" id="phone" name="phone" placeholder="503 999 9999">

                <label for="comments">Comments</label>
                <textarea id="comments" name="comments"></textarea>

            </div><!--/ Form Column -->

            <div class="submit-wrap"><input class="submit-form" type="submit" value="Register" /></div>

        </form><!--/ Form -->

The PHP looks like this:

<?php

$myemail = 'veeeeeech@gmail.com';

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$visitor_phone = $_POST['phone'];
$comments = $_POST['comments'];


$to = $myemail;

$email_subject = "$first_name\n $last_name\n registered for Winter Camp 2015";

$email_body = "They said: \n $comments". "Their phone number is $visitor_phone.\n".


$headers = "From: $myemail\n";

mail($to,$email_subject,$email_body,$headers);

header('Location: registered-thanks.html');

?>

Veech
  • 61
  • 1
  • 1
  • 3
  • 1
    How is your server set up? Do you have the required bits on your server (i.e. an MTA etc) installed and able to relay mail? – slugonamission Dec 18 '14 at 23:05
  • 1
    there is an error in your code. `$email_body =` doesn't end with a semi-colon. – RST Dec 18 '14 at 23:10

3 Answers3

1

Mail delivery is an inexact science from the programmer's POV. The system admin has a bit more control over it ... are you also the "box owner"?

You're not even checking to see if the mail() call failed:

$success = mail($to,$email_subject,$email_body,$headers);

if ($success) header('Location: registered-thanks.html');

else die("Could not send mail!!");

Of course that is way simplified ... better error handling would be a nice start.

Assuming that ($success) is returning true, it's then up to the system to do the right thing. Questions you'll need to ask include: is my PHP installation properly set up to do mail() ?

Does my web host allow it?

Is there a firewall blocking outbound SMTP connections?

What do the server logs say?

Did the mail get caught in a spam filter?

You can see why I say "an inexact science"....

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
  • Wow, I didn't realize it was much more complicated. I haven't gotten into PHP and was researching this and didn't catch any of these things. Thanks for the help though! If there is any place to be able to look over an entire walk through that'd be very helpful. – Veech Dec 18 '14 at 23:10
  • I suppose Google might be your friend, there. The rather long PHP.net documentation is a good place to start. For SMTP in general, look for something like "SMTP explanation". I mean, oftentimes mail() will just work, but if it doesn't, "the game is afoot" like Sherlock Holmes until it does ;) – Kevin_Kinsey Dec 18 '14 at 23:13
  • As another small issue, @Veech, mail headers should be separated with `\r\n`, not just `\n`. – TRiG Dec 18 '14 at 23:21
1

PHP's mail function will return true when the message has been accepted for delivery. This is different from actually delivering or guaranteeing delivery, as noted by Kkinsey.

If you have shell access to the server where your web files are hosted you can test out whether or not you can even send mail out by trying this.

mail -s "Test Email" veeeeeech@gmail.com

Enter a line of text, then on a line all by itself enter a single "." and hit return. Look for the mail in your gmail account.

You might also try using sendmail directly as a quick test.

sendmail veeeeeech@gmail.com
HeadCode
  • 2,770
  • 1
  • 14
  • 26
0

have you checked the spam folder? some services will reject the email if it doesnt have a reply-to header.

$myemail = 'veeeeeech@gmail.com';
$to = $myemail;

$headers = "From: $myemail \r\n"; 

$headers .= "Reply-To: $myemail \r\n";

$headers .= 'X-Mailer: PHP/' . phpversion();

$success = mail($to,$email_subject,$email_body,$headers);

if ($success) header('Location: registered-thanks.html');

else die("Foobar. Your mom.");
Todd
  • 5,314
  • 3
  • 28
  • 45