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

PHP Form Sadly Not Working...

I have been trying to implement a PHP Contact Form at the end of this page: http://www.rachelbeen.com/Elizabeth-Reverse Im not sure what Im doing wrong. It simply wont send the email. Any help would be greatly appreciated!

5 Answers

samiff
samiff
31,206 Points

I'm new to php, but I don't think anyone can help much unless you post your php code that deals with the form.

Very valid point!

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);


    if ($name == "" OR $email == "" OR $message == "") {
        echo "You must specify a value for name, email address, and message.";
        exit;
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            echo "There was a problem with the information you entered.";    
            exit;
        }
    }

    if ($_POST["address"] != "") {
        echo "Your form submission has an error.";
        exit;
    }

    require_once("class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
        echo "You must specify a valid email address.";
        exit;
    }


    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Message: " . $message;

    $mail->SetFrom($email, $name);
    $address = "rachelbeen@gmail.com";
    $mail->AddAddress($address, "Shirts 4 Mike");
    $mail->Subject    = "Shirts 4 Mike Contact Form Submission | " . $name;
    $mail->MsgHTML($email_body);

    if(!$mail->Send()) {
      echo "There was a problem sending the email: " . $mail->ErrorInfo;
      exit;
    }

    header("Location: contact.php?status=thanks");
    exit;
}
?>

Well this is only a part of the code it says you require a file that is "class.phpmailer.php" do you have that file you can post?

This file does not have a mail function attached! I believe its going to be in the class.phpmailer.php file if that file exists in your project?

James Barnett
James Barnett
39,199 Points

@Rachael - Take a look at this blog post on using PHPMailer to send email via gmail hopefully that will get you started.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

@Rachael - How the actual sending happens depends. I talk about this in the video on sending the email, 7:00-7:55. It sounds like your server needs to use SMTP. Take a look at the article James referenced earlier or the article on the Treehouse Blog that I mention in the video for more information: