include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');
致命错误:Class' PHPMailer'在第151行的C:\ Inetpub \ wwwroot \ php \ index.php中找不到
我将PHPMailerAutoload.php
放在与我的脚本相同的目录中。
有人可以帮我这个吗?
答案 0 :(得分:55)
现在所有答案都已过时。最新版本(截至2018年2月)不再有自动加载,PHPMailer应初始化如下:
<?php
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
require("/home/site/libs/PHPMailer-master/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx@xxxxx.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxxx@xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
答案 1 :(得分:11)
听起来不像使用该类所需的所有文件。我会重新开始:
require_once('phpmailer/PHPMailerAutoload.php');
答案 2 :(得分:7)
这只是命名空间。查看示例以供参考 - 您需要使用命名空间类或绝对引用它,例如:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
答案 3 :(得分:5)
对于那些仍然遇到问题的人来说,这回答了avs099给出的内容:
1.确保您安装了php_openssl.dll(否则在线查找并安装);
2.转到你的php.ini; find extension = php_openssl.dll enable it / uncomment
3.转到github并降低最新版本:6.0此时。
4.将主副本提取到更适合您的路径中(我建议使用与调用文件相同的目录)
现在将此代码复制到您的 foo-mailer .php中,并使用您的gmail stmp身份验证进行渲染。
require("/PHPMailer-master/src/PHPMailer.php");
require("/PHPMailer-master/src/SMTP.php");
require("/PHPMailer-master/src/Exception.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->Port = 465 ; //465 or 587
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
//Authentication
$mail->Username = "foo@gmail.com";
$mail->Password = "*******";
//Set Params
$mail->SetFrom("foo@gmail.com");
$mail->AddAddress("bar@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
免责声明:上面代码的原始版主是avs099,只是我的小输入。
请注意附加内容:
a)(PHPMailer \ PHPMailer)命名空间:名称冲突解决所需。
b)(require(“/ PHPMailer-master / src / Exception.php”);):它在avs099的代码中缺失因此aProgger遇到的问题,你需要该行来告诉邮件程序类哪里有异常班级位于。
答案 4 :(得分:3)
我建议您考虑获取composer
。 https://getcomposer.org
Composer使得第三方库变得更容易,并为所有这些库使用单个自动加载器。它还标准化了所有依赖项的位置,以及一些自动化功能。
将https://getcomposer.org/composer.phar下载到C:\Inetpub\wwwroot\php
删除C:\Inetpub\wwwroot\php\PHPMailer\
目录。
使用composer.phar
使用命令行执行
cd C:\Inetpub\wwwroot\php
php composer.phar require phpmailer/phpmailer
完成后,它将创建一个C:\Inetpub\wwwroot\php\vendor
目录以及所有phpmailer文件,并生成一个自动加载器。
接下来在主项目配置文件中,您需要包含自动加载文件。
require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';
vendor\autoload.php
将包含您使用$mail = new \PHPMailer;
有关PHPMailer软件包的更多信息,请访问https://packagist.org/packages/phpmailer/phpmailer
答案 5 :(得分:2)
从 2021-07 和 PHPMailer 6.5.0 开始,如果没有 Composer,您需要按以下顺序进行包含:
$rdir = str_replace("\\", "/", __DIR__); //Root Dir
require $rdir.'/PHPMailer/src/Exception.php';
require $rdir.'/PHPMailer/src/PHPMailer.php';
require $rdir.'/PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
您可能需要根据您的目录结构进行调整。
其余代码按预期工作。
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.yourserver.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'no-reply@example.com'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('mail@example.com');
$mail->addAddress('mail@example.com', 'Joe User'); //Add a recipient
$mail->addAddress('mail@example.com', 'Joe Doe'); //Name is optional
$mail->addAddress('mail@example.com', 'Optional Name'); //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 = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
答案 6 :(得分:1)
我遇到了同样的问题,除了好朋友avs099的PHPMailer 6.0版本略有不同之外,我知道自2018年2月以来的新版本PHPMailer不支持自动加载,并且在实例化方面存在严重问题MVC中带有名称空间的库,我将代码留给需要的人。
//Controller
protected function getLibraryWNS($libreria) {
$rutaLibreria = ROOT . 'libs' . DS . $libreria . '.php';
if(is_readable($rutaLibreria)){
require_once $rutaLibreria;
echo $rutaLibreria . '<br/>';
}
else{
throw new Exception('Error de libreria');
}
}
//loginController
public function enviarEmail($email, $nombre, $asunto, $cuerpo){
//Import the PHPMailer class into the global namespace
$this->getLibraryWNS('PHPMailer');
$this->getLibraryWNS('SMTP');
//Create a new PHPMailer instance
$mail = new \PHPMailer\PHPMailer\PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// $mail->SMTPDebug = 0; // 0 = off (for production use), 1 = client messages, 2 = client and server messages Godaddy POR CONFIRMAR
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//Whether to use SMTP authentication
$mail->SMTPAuth = true; // authentication enabled
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl'; //Seguridad Correo Gmail
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com"; //Host Correo Gmail
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465; //587;
//Verifica si el servidor acepta envios en HTML
$mail->IsHTML(true);
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'tumail@gmail.com';
//Password to use for SMTP authentication
$mail->Password = 'tucontraseña';
//Set who the message is to be sent from
$mail->setFrom('tumail@gmail.com','Creador de Páginas Web');
$mail->Subject = $asunto;
$mail->Body = $cuerpo;
//Set who the message is to be sent to
$mail->addAddress($email, $nombre);
//Send the message, check for errors
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
echo "Message has been sent";
return true;
}
答案 7 :(得分:0)
只是阅读您所写的内容,您还需要将文件class.phpmailer.php添加到您的目录中。
答案 8 :(得分:0)
PHPMailerAutoload
需要与class.phpmailer.php
这是我假设的PHPMailerAutoload
代码:
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
答案 9 :(得分:0)
Just download composer and install phpMailler autoloader.php
https://github.com/PHPMailer/PHPMailer/blob/master/composer.json
once composer is loaded use below code:
require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/PHPMailerAutoload.php");
$mail = new PHPMailer(true);
$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'youremail id';
$mail->Password = 'youremail password';
$mail_from = "youremail id";
$subject = "Your Subject";
$body = "email body";
$mail_to = "receiver_email";
$mail->IsSMTP();
try {
$mail->Host= "smtp.your.com";
$mail->Port = "Your SMTP Port No";// ssl port :465,
$mail->Debugoutput = 'html';
$mail->AddAddress($mail_to, "receiver_name");
$mail->SetFrom($mail_from,'AmpleChat Team');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->Send();
$emailreturn = 200;
} catch (phpmailerException $e) {
$emailreturn = $e->errorMessage();
} catch (Exception $e) {
$emailreturn = $e->getMessage();
}
echo $emailreturn;
希望这会奏效。
答案 10 :(得分:0)
我有很多类似的错误。确保您的setFrom电子邮件地址在$mail->setFrom()
答案 11 :(得分:0)
我解决了将文件class.phpmailer.php,class.smtp.php复制到文件PHPMailerAutoload.php的错误,当然,应该有用于发送电子邮件的文件。