Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Adam Duffield
Adam Duffield
30,494 Points

PHP mailto()

Hey Guys,

Been struggling for about 2 weeks now on how to get a proper email form working on my Portfolio, i've tried well over 10 examples found from the internet aswell as made my own from scratch and I come across 3 common problems.

Sometimes the email works but I can't get any submitted data through the POST method.

Sometimes it works in the browser but won't sent an email.

Sometimes It doesn't work at all on the browser or getting an email through.

Anybody got any dummy guides for a newbie to PHP wanting to make an email form? If it helps anyone answer this I've never actually downloaded PHP onto my computer, only through WAMP Server.

1 Answer

Andrew McCormick
Andrew McCormick
17,730 Points

You can start by copying and pasting this code into a php file, make sure it works and then start editing it form there. If this form doesn't work then maybe something is misconfigured in your WAMP seetings.
W3C Schools PHP mail()

<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback">
  </form>
  <?php 
} else {    // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    $from = $_POST["from"]; // sender
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    // message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // send mail
    mail("webmaster@example.com",$subject,$message,"From: $from\n");
    echo "Thank you for sending us feedback";
  }
}
?>

So basically here it is broken down:

We start with checking to see if we are seeing this page for the first time or after a user submitted the form. If they submitted the form, then we just need to mail the info and display a confirmation. Otherwise, we want to see the form

<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>

Next, if the user has not sent the form, aka $_POST['submit'] is not set. Then we want to display the form. Notice that the method is post and the then action is set to php echo $_SERVER["PHP_SELF"]. Also make sure form elements have name attributes so that they are available in the $_POST array.

  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback">
  </form>
  <?php 
}

Finally, if we are seeing this form after a submission, then we will skip displaying the form and rather get teh $_POST array, process the mail, and display a confirmation

else {    // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    $from = $_POST["from"]; // sender
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    // message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // send mail
    mail("webmaster@example.com",$subject,$message,"From: $from\n");
    echo "Thank you for sending us feedback";
  }
}
?>

hope that helps.

Adam Duffield
Adam Duffield
30,494 Points

Thanks for a try at this buddy, but I'm actually using PHP for my live site http://www.ajduff.co.uk/Contact.php but I was just curious if me having WAMP installed would intefere with that at all? I've tried your code and edited it for my email address and it won't work :(

Andrew McCormick
Andrew McCormick
17,730 Points

Adam, WAMP won't interfere at all with your live site. Can you post the code you have for your email.php page and we can take a look.

Adam Duffield
Adam Duffield
30,494 Points

Hey, I managed to get it working in the end with some random code off google, I think there was a problem with the name of my "FormProcess.php" file that was preventing the emails from sending all the time, Thanks for the quick support though Andrew!