I am facing this error "Fatal error: Uncaught Error: Class 'PHPMailer' not found" when I submit the appointment form. The code is working fine on localhost but not working on live server.
On the live server, I have created a directory named "phpmailer" on the root directory of the website where all files are located. The "phpmailer" directory has 4 files:
I also make a copy of the file "PHPMailerAutoload.php" out of the "phpmailer" directory, then I edit the file "PHPMailerAutoload.php" and change the path by putting phpmailer.
Here is the email code:
require 'PHPMailerAutoload.php';
require 'phpmailer/credentials.php';
$mail = new PHPMailer; // This line has an error of PHPMailer class not found
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.ipage.com';
$mail->SMTPAuth = true;
$mail->Username = EMAIL;
$mail->Password = PASS;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom(EMAIL, 'Symbiosis Home Care');
$mail->addAddress('babarabid123@gmail.com', 'Babar Ali');
$mail->addReplyTo(EMAIL);
$mail->Subject = "Enquiry Form - Symbiosis Home Care";
$mail->Body = 'New Enquiry Received';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else{
if (!empty($i_name)) {
$result='<div class="alert alert-success background-success">
<button aria-label="Close" class="close" data-dismiss="alert" type="button"><i class="fa fa-close"></i></button>Welcome <strong>' . $i_name .',</strong> Thanks For Contacting Us. We Will Get Back To You Soon.</div>';
echo $result;
}
else {}
}
Before I was using an outdated phpmailer library. Then I went to github website I searched for how to install the composer. After installing the composer, follow the steps given below:
Note: Remember, use namespaces/packages at the top of your code otherwise phpmailer will not work.
<!-- Contact/Appointment Form Start-->
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
include("database.php");
if(isset($_POST['contact_submit'])) {
$i_name = $_POST['name'];
$i_phone = $_POST['phone'];
$i_email = $_POST['email'];
$i_service = $_POST['service'];
$i_subject = $_POST['subject'];
$i_message = $_POST['message'];
$i_status = true;
// Date Time Settings
date_default_timezone_set('Asia/Dubai');
//$i_date = date("d-m-Y H:i:s");
$i_date = date("d-m-Y, g:i a"); //output => 12-01-2019, 5:29 pm
// Inserting Inquiry Records in Table
$sql = "insert into inquiry_tbl(name,phone,email,service,subject,message,submission_date,status) values('$i_name','$i_phone','$i_email','$i_service','$i_subject','$i_message','$i_date','$i_status')";
if($con->query($sql)){
$last_id = $con->insert_id;
// Email Code Start
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.ipage.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your email'; // SMTP username
$mail->Password = 'your password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('info@symbiosishomecare.com', 'New Enquiry');
$mail->addAddress('babarabid123@gmail.com', 'Babar Ali'); // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email Subject';
$mail->Body = 'Body Content';
$mail->AltBody = 'Alternate Body content';
$mail->send();
$result='<div class="alert alert-success background-success">
<button aria-label="Close" class="close" data-dismiss="alert" type="button"><i class="fa fa-close"></i></button>Welcome <strong>' . $i_name .',</strong> Thanks For Contacting Us. We Will Get Back To You Soon.</div>';
echo $result;
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
// Email Code End
}
else{
$sql_error = mysqli_error($con);
if (!empty($sql_error)) {
$result='<div class="alert alert-danger background-danger">
<button aria-label="Close" class="close" data-dismiss="alert" type="button"><i class="fa fa-close"></i></button> <strong>Error: </strong>'. $sql_error .'</div>';
echo $result;
}
else {}
}
}
else{}
mysqli_close($con);
?>