Fatal error: Uncaught Error: Class 'PHPMailer' not found
Fatal error: Uncaught Error: Class 'PHPMailer' not found
当我提交预约表格时,我遇到了这个错误“致命错误:未捕获错误:Class 'PHPMailer' 未找到”。该代码在本地主机上运行良好,但在实时服务器上运行不正常。
在live服务器上,我在所有文件所在的网站根目录下创建了一个名为“phpmailer”的目录。 “phpmailer”目录有 4 个文件:
- class.phpmailer.php
- class.smtp.php
- credentials.php
- 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 {}
}
您是否在您的实时系统上安装了 PHPMailer?
composer require phpmailer/phpmailer
如果不想安装Composer,可以手动添加PHPMailer。下载带有 PHPMailer 源代码的文件,然后将 PHPMailer 文件夹的内容复制到 PHP 配置中指定的 include_path 目录之一,并加载每个 class 手动归档:
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';
添加异常 class 将帮助您处理错误并调试它们。
在我使用过时的 phpmailer 库之前。然后我去 github 网站搜索如何安装 composer。安装 composer 后,请按照以下步骤操作:
- 转到您的项目根目录
- 按住 Shift 键并单击鼠标右键,然后单击 Window Powershell
- 写这个命令“composer require phpmailer/phpmailer”。执行命令需要2-3分钟。
- 当命令成功执行时,它会在该特定目录中创建一些文件和文件夹。在完成所有这些之后,您只需使用以下代码即可使您的 phpmailer 正常工作。
注意:请记住,在代码顶部使用 namespaces/packages,否则 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);
?>
当我提交预约表格时,我遇到了这个错误“致命错误:未捕获错误:Class 'PHPMailer' 未找到”。该代码在本地主机上运行良好,但在实时服务器上运行不正常。
在live服务器上,我在所有文件所在的网站根目录下创建了一个名为“phpmailer”的目录。 “phpmailer”目录有 4 个文件:
- class.phpmailer.php
- class.smtp.php
- credentials.php
- 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 {}
}
您是否在您的实时系统上安装了 PHPMailer?
composer require phpmailer/phpmailer
如果不想安装Composer,可以手动添加PHPMailer。下载带有 PHPMailer 源代码的文件,然后将 PHPMailer 文件夹的内容复制到 PHP 配置中指定的 include_path 目录之一,并加载每个 class 手动归档:
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';
添加异常 class 将帮助您处理错误并调试它们。
在我使用过时的 phpmailer 库之前。然后我去 github 网站搜索如何安装 composer。安装 composer 后,请按照以下步骤操作:
- 转到您的项目根目录
- 按住 Shift 键并单击鼠标右键,然后单击 Window Powershell
- 写这个命令“composer require phpmailer/phpmailer”。执行命令需要2-3分钟。
- 当命令成功执行时,它会在该特定目录中创建一些文件和文件夹。在完成所有这些之后,您只需使用以下代码即可使您的 phpmailer 正常工作。
注意:请记住,在代码顶部使用 namespaces/packages,否则 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);
?>