错误:
Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use.
下面是正在使用的代码
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/SMTP.php';
// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function SendSMTPemail(){
try {
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host='ssl';
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username='xy@xy.com';
$mail->Password='xxxxxxx';
$mail->setFrom('xy@xy.com');
$mail->AddAddress('yz@yz.com'); // SMTP password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // TCP port to connect to
$mail->addReplyTo('ab@ab.com', 'Information');
$mail->isHTML(true);
$mail->Subject = "Appointment Details";
$mail->Body = "Dear, <br> Please be informed that you have an appointment tommorow <br> Regards <br> ";
$mail->AltBody = '';
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
//echo "success";
// $globals['emailstatus'] =true;
// header("Location: sendEmail.php");
} catch (Exception $e) {
//echo "fail";
// $globals['emailstatus'] =false;
echo "Message could not be sent.";
}
}
SendSMTPemail();
Run Code Online (Sandbox Code Playgroud)
如果以下代码行被注释,
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/SMTP.php';
Run Code Online (Sandbox Code Playgroud)
然后它显示以下错误:
Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found
Run Code Online (Sandbox Code Playgroud)
如果以下代码行被注释,
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
Run Code Online (Sandbox Code Playgroud)
然后它显示以下错误:
Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use
Run Code Online (Sandbox Code Playgroud)
我曾经使用过 PHPMailer,之前使用 Gmail 的 SMTP,运行得很好。有人可以帮忙解释为什么尽管所需的文件位于正确的目录中却显示此错误吗?
调查结果(编辑) 我意识到 wp_content 文件夹以及 wp_includes 中也存在一个 PHPMailer 文件夹。很久以前,当我使用 PHPmailer for gmail 时,我已经将 PHPMailer 文件夹上传到了 wp_content 。我的文件中有该文件夹的多个副本。一个位于供应商文件夹的根目录中,由composer和其他人在wp_content和wp_includes中创建。wp_includes 很奇怪,因为我不知道作曲家是否创建了或者是什么......我应该做什么。我想只使用 wp_content 中的一个,因为当我使用 gmail SMTP @Synchro 使用它时它工作正常
尝试类似的东西
if (!class_exists('PHPMailer\PHPMailer\Exception'))
{
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/SMTP.php';
}
Run Code Online (Sandbox Code Playgroud)