Unable load files within library in Codeigniter

2019-09-08 09:47发布

问题:

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 :

Message: Call to undefined method PHPMailerOAuth::isSMTP()

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.

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!";
    }
}

And it's how I load this library within Controller's function :

$this->load->library('PHPMailer');
$this->phpmailer->singleMail($setFromTitle,$setToMail,$setToName,$setSubject,$setMessage);

回答1:

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
     }
}

So don't named your class to PHPMailer or something like this which probably has defined in core functions!