无法在Codeigniter中的库中加载文件 [英] Unable load files within library in Codeigniter
问题描述
我已经在Controller内编写了一个函数来使用PHPMailer,并且工作正常.之后,我经常需要此函数,最终我决定编写库.但是当这样做并在Controller中加载Library时,它会出现如下错误:
I've written a function within Controller to use PHPMailer and it works fine. After that I need this Function frequently and eventually I decide to write Library. But when do this and load Library within Controller, It has error like this :
消息:调用未定义的方法PHPMailerOAuth :: isSMTP()
Message: Call to undefined method PHPMailerOAuth::isSMTP()
毕竟,我发现路径存在问题!我已经尝试了您的想法,但没有成功!
我已经尝试将Controller作为库
我试过include()
,require_once()
,require
,有无APPATH
.
我已经尽我所能搜索了.甚至我都读过Third Party
,但我听不懂.
有什么想法可以在Library中加载PHPMailer文件吗?
预先谢谢你.
After all I've figured out there is problem with path! I've tried what ever you're thinking but it doesn't work!
I've tried Controller as library
I've tried include()
,require_once()
,require
, with or without APPATH
.
I've search as much as I could. Even I've read about Third Party
but I don't understand.
Is there any idea to load PHPMailer file within Library?
Thank you in advance.
这是一个名为PHPMailer.php的库
It's library called PHPMailer.php
class PHPMailer {
public function singleMail($setFromTitle,$setToMail,$setToName,$setSubject,$setMessage)
{
date_default_timezone_set('Etc/UTC');
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
require 'vendor/autoload.php';
$mail = new PHPMailerOAuth;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$mail->oauthUserEmail = "XXX@gmail.com";
$mail->oauthClientId = "XXX";
$mail->oauthClientSecret = "XXX";
$mail->oauthRefreshToken = "XXX";
$mail->setFrom('XXXy@zounkan.com', $setFromTitle);
$mail->addAddress($setToMail, $setToName);
$mail->Subject = $setSubject;
$mail->msgHTML($setMessage);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
这就是我如何在Controller的函数中加载此库的方法:
And it's how I load this library within Controller's function :
$this->load->library('PHPMailer');
$this->phpmailer->singleMail($setFromTitle,$setToMail,$setToName,$setSubject,$setMessage);
推荐答案
根据PHPMailer的所有者,他帮助了我,并最终解决了这个问题! 它应该发生在每个程序员身上,当我修复它时,我感到惊讶!问题出在类名上!我将PHPMailer类定义为Library的类,这是不允许的!所以上面的代码(我已经提到过)应该尝试一下,它也可以正常工作:
According to PHPMailer's owner, he helped me and eventually solve this problem! It should happen for every programmer and when I fixed it I surprised! The problem was about the class name! I define PHPMailer class as Library's class and it's not allowed! So intead of code above (That I'v mentioned) I should try this and it works fine as well :
class Sendingmail {
public function singleMail($setFromTitle,$setToMail,$setToName,$setSubject,$setMessage) {
// Rest of codes
}
}
因此,请勿将您的类命名为PHPMailer或可能已在核心函数中定义的类似名称!
So don't named your class to PHPMailer or something like this which probably has defined in core functions!
这篇关于无法在Codeigniter中的库中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!