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 Build a Simple PHP Application Wrapping Up The Project Sending The Contact Form Email

rigzin norboo
rigzin norboo
11,059 Points

Email system is not working.

I am following exact same from video and when i submit the form , it loads the page for some time and do nothing. In my class.phpmailer.php msgHTML() have three parameters but we are sending only one. could that be the reason for an error. May be i made mistake in my code.

<?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 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 from submission has an error.";
    }

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

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

    // TODO: send Email

    $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 = "chimedpaldan@gmail.com";
    $mail->addAddress($address, "Shirts 4 Mike");
    $mail->Subject = "Shirts 4 Mike Contact From 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;
}
?>

1 Answer

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,803 Points

Chimed: Wish I could help, but I'm not too familiar with PHPMailer, so I'd mostly just be guessing at this point. If you still don't have an answer in a day or two, I'll try to do some reading to help you out.

Good luck!