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

Can somebody tell me why this php code to send an email isn't working?

Anybody know why this code is not working at sending me an email when i fill out the form?

<?php

include("header.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING)); $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL)); $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

if($name == "" || $email == "" || $details == ""){ echo "You must fill out all the fields in the form please go back and try again."; exit; }

require("class.phpmailer.php");

$mail = new PHPMailer;

if(!$mail->ValidateAddress($email)) { echo "Invalid Email"; exit; }

$email_body = ""; $email_body .= "Name: " . $name . "\n"; $email_body .= "Email: " . $email . "\n"; $email_body .= "Details: " . $details . "\n";

//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('plathavpro@platinumhavocproductions.com', 'Plat Hav Pro');     // Add a recipient




//Content
$mail->isHTML(false);                                  // Set email format to HTML

$mail->Subject = 'Price Quote Request from' . $name;
$mail->Body    = $email_body;

if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; }

header("location:pricequote.php?status=thanks"); }

if (isset($_GET["status"]) && $_GET["status"] == "thanks") { echo "<p1> Your Free Price Quote will be emailed to you within 24 hours. Thanks for inquiring with Sitespressions.</p1>"; } else { ?>

<form action="pricequote.php" method="post">

  <legend class="formtitle">Please fill out this form to see if you qualify</legend>
 <fieldset>

    <label for="name">Your name?</label>
  <input type="text" id="name" name="name">

    <label for="email">Your email?</label>      
  <input type="email" id="email" name="email">

    <label for="details">Describe the website you want built and please include who will be hosting it:</label>
  <textarea id="details" name="details"></textarea>

  <button type="submit">Get a free quote.</button>
   </fieldset>








</form>

<?php } ?>

2 Answers

Nathan Thomas
Nathan Thomas
13,477 Points

Hi Arthur,

Is there meant to be a curly brace after the header function call? When I place this code in an editor I receive an error, syntax error, unexpected "}" This may be causing an issue.

Best, Nathan.

Doesn't work without the }

Everything works fine in the treehouse workspace I was able to send mail to my treehouse console. When I put this code on my real website its not working I tried adding the smtp but that didn't help either. Very frustrating