简体   繁体   中英

Undefined property: Controller::$Mail_library in codeigniter

I am integrating phpmailer but can't load my library what's going here I have a problem that I can't load my library in my controller

I got this error:

Message: Undefined property: Welcome::$Mail

I am facing these issues, please suggest

Severity: Notice

Message: Undefined property: Welcome::$Mail

Filename: controllers/Welcome.php

Line Number: 352

library file name as Mail.php

Class names and file names must be matched but getting error

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    class Mail
    {
        public function __construct(){
            log_message('Debug', 'PHPMailer class is loaded.');
        }

        public function load(){
            // Include PHPMailer library files
            require_once APPPATH.'third_party/phpmailer/src/Exception.php';
            require_once APPPATH.'third_party/phpmailer/src/PHPMailer.php';
            require_once APPPATH.'third_party/phpmailer/src/SMTP.php';

            $mail = new PHPMailer\PHPMailer\PHPMailer(true);
            return $mail;
        }
    }

    ?>

Welcome Controller Code [Welcome.php]

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Welcome extends CI_Controller {

        public function __construct()
        {
            parent::__construct();

            $this->load->library('Mail');

        } 

        function email_user($full_name, $email, $passwordplain){

            $this->load->library('Mail'); // error show this line 

            $mail = $this->Mail->load();

            $mail->isSMTP();
            $mail->Host     = 'ssl://smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'test@gmail.com';
            $mail->Password = 'password';
            $mail->SMTPSecure = 'tls';
            $mail->Port     = 465;

            $mail->setFrom('example@gmail.com');
            $mail->addAddress($email);
            $mail->addCC('example@gmail.com');
            $mail->addBCC('example@gmail.com');
            $mail->Subject = 'Send Email via SMTP using password';
            $mail->isHTML(true);
            $mail_message = "<h1>Send HTML Email using SMTP in CodeIgniter</h1>
            <p>This is a test email sending using SMTP mail server with PHPMailer.</p>";
            $mail_message = 'Dear ' . $full_name. ',' . "\r\n";
            $mail_message .= 'Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>' . $passwordplain . '</b>' . "\r\n";
            $mail_message .= '<br>Please Update your password.';
            $mail_message .= '<br>Thanks & Regards';
            $mail_message .= '<br>Your company name';
            $mail->Body = $mail_message;


            if(!$mail->send()){
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            }else{
                echo 'Message has been sent';
            }
        }
    }
?>

Try this code:

$this->load->library('mail');

$mail = $this->mail->load(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM