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 trialMichael Strand
10,897 PointsFatal error: Class 'PHPMailer' not found
I am getting this error upon submitting my form. I'm not quite sure what I'm over looking. If I could get some direction and help on this, that would be wonderful. Thanks!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fname = trim($_POST["fname"]);
$lname = trim($_POST["lname"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
$foundus = trim($_POST["foundus"]);
if ( $fname == "" OR $lname == "" OR $email == "" OR $message == "" ) {
echo "You need to enter your name, email, and a 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["blank"] != "" ) {
echo "Your form has an error.";
exit;
}
require_once ("http://siteaddress.com/wp-includes/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: " . $fname . " " . $lname . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message . "<br>";
$email_body = $email_body . "How did you find us? " . $foundus;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "username";
$mail->Password = "password";
$mail->SetFrom($email, $fname . " " . $lname);
$address = "email@emailaddress.com";
$mail->AddAddress($address, "Company");
$mail->Subject = "Company Form Submission | " . $fname . " " . $lname;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header ("Location: http://siteaddress.com/contact/?status=thanks");
exit;
}
?>
<?php get_header(); ?>
<div id="content" class="container">
<div class="block"></div>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p class="">Thank you for contacting us. We will be in contact with you soon.</p>
<?php } else { ?>
<div class="form-wrapper">
<form method="post" action="http://siteaddress.com/contact/" id="form-contact">
<div class="field-list clearfix">
<fieldset class="form-item fields name">
<div class="title">Name *</div>
<div class="field first-name">
<label class="caption" for="fname">
<input class="field-element" type="text" name="fname" id="fname" autofocus>
First Name
</label>
</div>
<div class="field last-name">
<label class="caption" for="lname">
<input class="field-element" type="text" name="lname" id="lname">
Last Name
</label>
</div>
</fieldset>
<div class="form-item field">
<label class="title" for="email">Email Address *</label>
<input class="field-element" type="email" name="email" id="email">
</div>
<div class="form-item field">
<label class="title" for="message">Project *</label>
<textarea class="field-element" name="message" id="message"></textarea>
</div>
<div class="form-item field">
<label class="title" for="foundus">How did you find us?</label>
<input class="field-element" type="text" name="foundus" id="foundus">
</div>
<div style="display:none">
<label for="blank">Address</label>
<input type="text" name="blank" id="blank">
</div>
</div>
<div class="form-button-wrapper">
<input class="form-button" type="submit" value="Send">
</div>
</form>
<?php } ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="bottom-section"><?php the_content(); ?></div>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
2 Answers
Donny van Walsem
1,815 PointsCan you show your phpmailer class?
EDIT: this was meant to be a comment but i don't see how to remove it.
Donny van Walsem
1,815 PointsI'm pretty sure you just forgot to include PHPmailer file in your page.
Simon Coates
28,694 PointsIf anyone ever looks at this, i think you need to include the complete PHPmailer library and require its autoload file. As is (just including PHPmailer), there doesn't seem to be any mechanism by which SMTP class is available to the PHPmailer class, when it attempts to create one.
Michael Strand
10,897 PointsMichael Strand
10,897 Points