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 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,815 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!