致命错误:未捕获错误:未找到类“ PHPMailer”

时间:2020-07-20 08:19:29

标签: php email smtp phpmailer

我提交约会表单时遇到此错误“致命错误:未捕获的错误:未找到类'PHPMailer'”。该代码在localhost上可以正常运行,但在实时服务器上不能正常工作。

在实时服务器上,我在所有文件所在的网站的根目录上创建了一个名为“ phpmailer”的目录。 “ phpmailer”目录包含4个文件:

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

我还从“ phpmailer”目录中复制了文件“ PHPMailerAutoload.php”,然后编辑文件“ PHPMailerAutoload.php”并通过放置phpmailer来更改路径。

这是电子邮件代码:

    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 {}
    }

2 个答案:

答案 0 :(得分:1)

您是否在实时系统上安装了PHPMailer?

composer require phpmailer/phpmailer

如果您不想安装Composer,则可以手动添加PHPMailer。使用PHPMailer源代码下载文件,然后将PHPMailer文件夹的内容复制到PHP配置中指定的include_path目录之一,并手动加载每个类文件:

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';

添加异常类将帮助您处理错误并对其进行调试。

答案 1 :(得分:-1)

在我使用过时的phpmailer库之前。然后,我去了github网站,搜索如何安装composer。安装作曲家之后,请按照以下步骤操作:

  1. 转到您的项目根目录
  2. 按Shift +右键单击,然后单击Window Powershell
  3. 将此命令写为“ composer require phpmailer / phpmailer”。执行命令将需要2-3分钟。
  4. 命令成功执行后,它将在该特定目录中创建一些文件和文件夹。完成所有这些之后,您只需使用以下代码即可使phpmailer正常工作。

注意:请记住,请在代码顶部使用名称空间/包,否则phpmailer将无法工作。

<!-- 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);
?> 
相关问题