php 致命错误:找不到“PHPMailer”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28906487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Class 'PHPMailer' not found
提问by iori
- I tried :
include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');
- 我试过 :
include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');
Fatal error: Class 'PHPMailer' not found in C:\Inetpub\wwwroot\php\index.php on line 151
致命错误:在第 151 行的 C:\Inetpub\wwwroot\php\index.php 中找不到“PHPMailer”类
I place the PHPMailerAutoload.php
in the same directory as my script.
我将它PHPMailerAutoload.php
放在与我的脚本相同的目录中。
Can someone help me with this ?
有人可以帮我弄这个吗 ?
回答by avs099
all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:
现在所有的答案都过时了。大多数当前版本(截至 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("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("[email protected]");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
回答by Drew
Doesn't sound like all the files needed to use that class are present. I would start over:
听起来不像使用该类所需的所有文件都存在。我会重新开始:
- Download the package from https://github.com/PHPMailer/PHPMailerby clicking on the "Download ZIP" button on the far lower right of the page.
- extract the zip file
- upload the language folder, class.phpmailer.php, class.pop3.php, class.smtp.php, and PHPMailerAutoload.php all into the same directory on your server, I like to create a directory on the server called phpmailer to place all of these into.
- Include the class in your PHP project:
require_once('phpmailer/PHPMailerAutoload.php');
- 单击页面最右下方的“下载 ZIP”按钮,从https://github.com/PHPMailer/PHPMailer下载包。
- 解压压缩文件
- 将语言文件夹class.phpmailer.php、class.pop3.php、class.smtp.php和PHPMailerAutoload.php都上传到你服务器上的同一个目录中,我喜欢在服务器上创建一个名为phpmailer的目录来放置所有这些成。
- 在您的 PHP 项目中包含该类:
require_once('phpmailer/PHPMailerAutoload.php');
回答by RickShaw
This answers in an extension to what avs099 has given above, for those who are still having problems:
对于那些仍然有问题的人,这是对 avs099 上面给出的内容的扩展:
1.Makesure that you have php_openssl.dll installed(else find it online and install it);
1.确保你已经安装了php_openssl.dll(否则网上找来安装);
2.Go to your php.ini; find extension=php_openssl.dll enable it/uncomment
2.转到你的php.ini;找到 extension=php_openssl.dll 启用它/取消注释
3.Go to githuband downland the latetest version :6.0 at this time.
3.此时去github下载最新版本:6.0。
4.Extract the master copy into the path that works better for you(I recommend the same directory as the calling file)
4.将主副本解压到更适合您的路径中(我推荐与调用文件相同的目录)
Now copy this code into your foo-mailer.php and render it with your gmail stmp authentications.
现在将此代码复制到您的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 = "[email protected]";
$mail->Password = "*******";
//Set Params
$mail->SetFrom("[email protected]");
$mail->AddAddress("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
Disclaimer:The original owner of the code above is avs099 with just my little input.
免责声明:以上代码的原始所有者是 avs099,只是我的一点点投入。
Take note of the additional:
注意额外的:
a) (PHPMailer\PHPMailer) namespace:needed for name conflict resolution.
a) (PHPMailer\PHPMailer) 命名空间:需要解决名称冲突。
b) The (require("/PHPMailer-master/src/Exception.php");):It was missing in avs099's code thus the problem encountered by aProgger,you need that line to tell the mailer class where the Exception class is located.
b) (require("/PHPMailer-master/src/Exception.php");): 它在avs099的代码中丢失,因此aProgger遇到的问题,您需要该行告诉邮件程序类Exception类所在的位置.
回答by Harshi Srivastava
This is just namespacing. Look at the examples for reference - you need to either use the namespaced class or reference it absolutely, for example:
这只是命名空间。查看示例以供参考 - 您需要使用命名空间类或绝对引用它,例如:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
回答by fyrye
I suggest you look into getting composer
. https://getcomposer.orgComposer makes getting third-party libraries a LOT easier and using a single autoloader for all of them. It also standardizes on where all your dependencies are located, along with some automatization capabilities.
我建议你考虑获取composer
. https://getcomposer.orgComposer 使获取第三方库变得更加容易,并且所有这些库都使用一个自动加载器。它还标准化了所有依赖项的位置,以及一些自动化功能。
Download https://getcomposer.org/composer.pharto C:\Inetpub\wwwroot\php
下载https://getcomposer.org/composer.phar到C:\Inetpub\wwwroot\php
Delete your C:\Inetpub\wwwroot\php\PHPMailer\
directory.
删除您的C:\Inetpub\wwwroot\php\PHPMailer\
目录。
Use composer.phar
to get the phpmailer package using the command line to execute
使用composer.phar
命令行获取phpmailer包执行
cd C:\Inetpub\wwwroot\php
php composer.phar require phpmailer/phpmailer
After it is finished it will create a C:\Inetpub\wwwroot\php\vendor
directory along with all of the phpmailer files and generate an autoloader.
完成后,它将创建一个C:\Inetpub\wwwroot\php\vendor
包含所有 phpmailer 文件的目录并生成一个自动加载器。
Next in your main project configuration file you need to include the autoload file.
接下来在您的主项目配置文件中,您需要包含自动加载文件。
require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';
require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';
The vendor\autoload.php
will include the information for you to use $mail = new \PHPMailer;
在vendor\autoload.php
将包括信息供您使用$mail = new \PHPMailer;
Additional information on the PHPMailer package can be found at https://packagist.org/packages/phpmailer/phpmailer
有关 PHPMailer 包的更多信息,请访问https://packagist.org/packages/phpmailer/phpmailer
回答by Sanjoy
I had a number of errors similar to this. Make sure your setFrom email address is valid in $mail->setFrom()
我有许多与此类似的错误。确保您的 setFrom 电子邮件地址在$mail->setFrom()
回答by Edgard Hinostroza
I was with the same problem except with a slight difference, the version of PHPMailer 6.0, by the good friend avs099 I know that the new version of PHPMailer since February 2018 does not support the autoload, and had a serious problem to instantiate the libraries with the namespace in MVC, I leave the code for those who need it.
我遇到了同样的问题,除了略有不同之外,PHPMailer 6.0 的版本,由好朋友 avs099 我知道自 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 = '[email protected]';
//Password to use for SMTP authentication
$mail->Password = 'tucontrase?a';
//Set who the message is to be sent from
$mail->setFrom('[email protected]','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;
}
回答by Wiiklats
I resolved error copying the files class.phpmailer.php , class.smtp.php to the folder where the file is PHPMailerAutoload.php, of course there should be the file that we will use to send the email.
我解决了将文件 class.phpmailer.php , class.smtp.php 复制到文件为 PHPMailerAutoload.php 的文件夹中的错误,当然应该有我们将用来发送电子邮件的文件。
回答by Deepak Singh
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;
Hope this will work.
希望这会奏效。
回答by Calvin K
Just from reading what you have written, you will need to add the file class.phpmailer.php to your directory as well.
仅通过阅读您所写的内容,您还需要将文件 class.phpmailer.php 添加到您的目录中。