Fatal error: Class 'PHPMailer' not found in .../contact/mailtest/process.php on line 8

qwaz picture qwaz · Sep 12, 2013 · Viewed 12.7k times · Source

I'm trying to make PHPMailer work, but it keeps giving me this error:

Fatal error: Class 'PHPMailer' not found in /home/a4588543/public_html/contact/mailtest/process.php on line 8.

and line 8 is:

$mail = new PHPMailer();

Here's the code:

<?php

$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "mysmtp-server";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "pass"; // SMTP password

$mail->From = "[email protected]";
$mail->FromName = "Online Request";
$mail->AddAddress("[email protected]");                  // name is optional


$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Contact Form";
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

Answer

Peter B picture Peter B · Dec 29, 2014

If you have included all of PHPMailer's source files in the same directory, and you're using the latest version... I recommend using

require_once('PHPMailerAutoload.php');
$mail = new PHPMailer();

The Autoload file is a great resource which saves a lot of effort when setting it up.