-1

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:

  1. class.phpmailer.php
  2. class.smtp.php
  3. credentials.php
  4. PHPMailerAutoload.php

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 {}
    }
Babar Ali
  • 1
  • 1
  • 5
  • You added a comment on your answer which indicates you need to install the missing library. Add the details how you installed php on your server so you can get a complete answer to you problem. – MadMike Jul 20 '20 at 09:05
  • Please [edit](https://stackoverflow.com/posts/62991310/edit) your question to add the relevant details. Adding details as a comment makes it hard for others to find the relevant information. – MadMike Jul 20 '20 at 09:26
  • Thanks for your reply :) 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: 1. class.phpmailer.php 2. class.smtp.php 3. credentials.php 4. PHPMailerAutoload.php 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. that's all I did sir. – Babar Ali Jul 20 '20 at 09:30
  • 1
    You still need to *click* the [edit](https://stackoverflow.com/posts/62991310/edit) link and add the information in your question though... – MadMike Jul 20 '20 at 09:34
  • I removed all comments, Is that okay now? – Babar Ali Jul 20 '20 at 09:36
  • No, that is *not* what I meant. I meant the information about the "phpmailer"-directory and such: *Add* that information to your *question text* below or above your code. – MadMike Jul 20 '20 at 09:42
  • What have you tried to debug the problem? – Nico Haase Jul 20 '20 at 10:10
  • I have used this check to debug the problem. if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } – Babar Ali Jul 20 '20 at 12:08
  • @MadMike I put the information about "phpmailer" directory in the question as you ask me to do. – Babar Ali Jul 20 '20 at 12:10
  • I have still a hard time helping you out. Can you please add the following information to your question. Is this a Windows- or a Linux-Server? With what tool do you upload and edit the files? – MadMike Jul 20 '20 at 15:32
  • Thanks for helping me sir, I fixed that issue myself ;) – Babar Ali Jul 21 '20 at 04:19
  • I have used the latest version of phpmailer from GitHub. – Babar Ali Jul 21 '20 at 04:21
  • Please add your solution as a answer. Like this others seeking help might find this question and answer useful. – MadMike Jul 21 '20 at 12:15

3 Answers3

1

Did you install PHPMailer on your livesystem?

composer require phpmailer/phpmailer

If you don’t want to install Composer, you can add PHPMailer manually. Download files with PHPMailer source code, then copy the contents of the PHPMailer folder to one of the include_path directories specified in your PHP configuration, and load each class file manually:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

Adding Exception class will help you handle errors and debug them.

Sascha
  • 4,576
  • 3
  • 13
  • 34
0

You need to modify the line :

$mail = new PHPMailer;  // This line has an error of PHPMailer class not found 

with

    $mail = new PHPMailer/PHPMailer/PHPMailer;  

Also similar changes to this line:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; PHPMailer::ENCRYPTION_SMTPS encouraged

Change to:

$mail->SMTPSecure = PHPMailer/PHPMailer/PHPMailer::ENCRYPTION_STARTTLS;  

this should resolve your issue.

aLamp
  • 59
  • 1
  • 12
-1

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:

  1. Go to your project root directory
  2. Press Shift + Right-click and then click on Window Powershell
  3. Write this command "composer require phpmailer/phpmailer". It will take 2-3 minutes to execute the command.
  4. When command executes successfully, it will create some files and folders in that particular directory. After all of this, you just have to use the following code to make your phpmailer work.

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);
?> 
Babar Ali
  • 1
  • 1
  • 5
  • How can I secure my data and website, please suggest. Actually, I am a frontend developer. I don't have enough knowledge of backend development. – Babar Ali Jul 24 '20 at 13:19
  • You can find an explanation here https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement and here https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Dharman Jul 24 '20 at 13:20