Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: