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 trialAlfred Davis
12,198 PointsPHP Integrating Validation Errors course - error message.
Hello,
I am currently on the PHP track using TreeHouse workspaces to follow the exercises . In the PHP Integrating Validation Errors course I receive the following error message: "There was a problem sending the email: Could not instantiate mail function. " I downloaded and used the course starter files and made all instructed changes. Is this issue due to me using a workspace instead of an localhost environment? My php code for my contact.php page is below:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must specify a value for name, email address, and message.";
}
if(!isset($error_message)){
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) &&!$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
if(!isset($error_messsage)){
$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 = "orders@shirts4mike.com";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()) {
header("Location: contact.php?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<?php
if(!isset($error_message)){
echo " <p>I’d love to hear from you! Complete the form to send me an email.</p>";
}else{
echo '<p class="message">' . $error_message . '</p>';
}
?>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name" value="<?php if(isset($name)){echo htmlspecialchars($name);} ?>"</inpu>
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email" value="<?php if(isset($email)){echo htmlspecialchars($email);} ?>">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"><?php if(isset($message)){echo htmlspecialchars($message);} ?></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<p>Humans (and frogs): please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<?php include('inc/footer.php') ?>
8 Answers
Salman Akram
Courses Plus Student 40,065 PointsHi Alfred,
I think, there is some issues on your require path, make sure double check require() command and path. The best way is to use an autoloader which can be configured to search certain paths for the required classes.
Please find this in your code that should be replaced with new path.
require_once("inc/phpmailer/class.phpmailer.php");
New path
require_once(ROOT_PATH . "inc/phpmailer/PHPMailerAutoload.php");
Alfred Davis
12,198 PointsSalman,
Thank you for the reply! I made the suggested edit and now after I submit the form I receive the following error: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" - "http://port-80-8v798liadg.treehouse-app.com/contact.php"
I have changed the code to what you see below:
require_once(ROOT_PATH . "inc/phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
Do you any other suggestions?
Salman Akram
Courses Plus Student 40,065 PointsAh I see. Weird. Please check your PHP error log that will explain problems in your script. Last time, I even tried to include PHPMailerAutoload which is working fine.
I feel that it sounds like server doesn't have the mail() function or something missing. I can suggest to take a look at this - PHPMailer
Alfred Davis
12,198 PointsSalman-
Do you think that the issue can be related to me using the TreeHouse workspace instead of a localhost environment?
Salman Akram
Courses Plus Student 40,065 PointsYes, possible likely.
thomascawthorn
22,986 PointsI would stick with this for now:
<?php
require_once("inc/phpmailer/class.phpmailer.php");
because this:
<?php
require_once(ROOT_PATH . "inc/phpmailer/PHPMailerAutoload.php");
doesn't exist in the project files.
If you didn't pick up any errors saying something like "could not include file xyz", it sounds like you're looking in the right place for the right file.
This set of project files ship with only the php mailer class file: 'class.phpmailer.php', and do not include the autoloader. I'm sure if you wanted to update the project files with the latest version of php mailer, you could jump onto github and download - but I would leave that until you've got this basic, slightly outdated version working first. I.e. I don't think the error you have is related to the file you're including or how you're including it.
try this:
<?php
require_once(ROOT_PATH ."inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
echo "<pre>";
var_dump($mail);
exit;
What's the output on the screen?
Alfred Davis
12,198 PointsHello Tom,
Thank you for the assistance but unfortunately I am still receiving the same error. I download the “class.phpmailer.php” from https://github.com/PHPMailer/PHPMailer and then uploaded the file to my workspace. Below I have included screenshots of the following; FireFox browser console error message, Chrome console error message and my workspace file structure. I am sure that this error is an oversight on my part so hopefully the screenshots will help.
FireFox browser console error message: https://drive.google.com/file/d/0B7lGRkQkCukdeTJvdHNFR2gwNzg/view?usp=sharing
Chrome console error message: https://drive.google.com/file/d/0B7lGRkQkCukdelRwUXY2d2dtVEk/view?usp=sharing
Workspace file structure: https://drive.google.com/file/d/0B7lGRkQkCukdWjVYaXY1aV9DVDQ/view?usp=sharing
Updated Code
<?php
require_once(ROOT_PATH ."inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
echo "<pre>";
var_dump($mail);
exit;
Thank you again!
thomascawthorn
22,986 PointsI imagine the 'failed to load resource' error is coming back because the php files you're trying to load contain warnings / errors. I've edited you new code above to resemble only the most important info for this thread.
I would either use the initial mailer class that was downloaded with the project files (that's what I would do - just to get it over the line) or use the class loader as suggested by Salman Ak.
You also need to make sure that this path:
<?php
require_once(ROOT_PATH ."inc/phpmailer/class.phpmailer.php");
is correct for your project! i.e. do you need an additional forward slash? Is the constant being set correctly?
<?php
require_once(ROOT_PATH ."/inc/phpmailer/class.phpmailer.php");
does it work without the constant?
<?php
require_once("inc/phpmailer/class.phpmailer.php");
Leave no stone unturned!
You can also try on a local server - which operating system do you use?