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

Tamas Dukai
Tamas Dukai
6,103 Points

Error with PHP mailer

Following the video I am getting this error message: Message could not be sent.Mailer Error: Could not instantiate mail function.

Here is my code:

<?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 "Please fill in the required fields: Name, Email and Details";
    exit;
}

if($_POST["address"] != "") {
    echo "Bad form input!";
    exit;
}

require("includes/phpmailer/class.phpmailer.php");

$mail = new PHPMailer;

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

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

$mail->setFrom($email, $name);
$mail->addAddress('dtommy79@localhost', 'Tom');     // Add a recipient

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

$mail->Subject = 'Personal Medai Library Suggestion form' . $name;
$mail->Body    = $email_body;

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

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

}