Code-Igniter: load & send email in a library

前端 未结 2 1695
礼貌的吻别
礼貌的吻别 2021-01-26 09:49

I have a library in CodeIgniter called \"someclass\".

class someclass extends CI_Controller {

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


        
相关标签:
2条回答
  • 2021-01-26 10:23

    You can have 2 situations in my vision.

    1. First you can use a helper. Create a .php file (ex: general_helper.php) which can be autoloaded or loaded where you will use it. Autoload if from config/autoload.php with 'general' name.

    Here, you can create a method called sendEmail

    function sendEmail($naar=0,$onderwerp=0,$bericht=0)
    {
        $CI =& get_instance();
    
            $CI->load->library('email');               
            //or autoload it from /application/config/autoload.php
    
            $CI->email->from('tester@test.com', 'test');
            $CI->email->to($to);
            $CI->email->subject($onderwerp);
            $CI->email->message("Test
    
                bericht :
                ".$bericht);
    
            $CI->email->send();
    
    }
    

    Load your custom library in your controller and call id with sendEmail(....)

    1. You can extend native email library class and create a method inside that class called sendEmail for example:

      class MY_Email extends CI_Email { public function _construct() { parent::_construct(); } public function sendEmail () ..... etc. }

    From your controller load native library class, use $this->load->library('email') and send your email by calling $this->email->sendEmail(...)

    0 讨论(0)
  • 2021-01-26 10:32

    You're doing it all wrong! Are you trying to write your own library? Or are you trying to write a controller for a page?

    If you're trying to write a controller, you don't need to run the get_instance() function. Simply extends the CI_Controller class, load the appropriate library, in this case it's email, and use it according to the documentation. Also, controllers are to be put inside the controllers folder.

    But if you're trying to write a library, you can't extend from the CI_Controller class. You'll also need to grab the instance of CodeIgniter using the get_instance() function and store it in a variable. After that simply use the object to load the email library and send the emails.

    Also, I don't think you can call a method with an underscore prefix from that class. I'm talking about $this->_assign_libraries(). In CodeIgniter, a method prefixed with an underscore is a private function. Thus, you can't access it from within your own class. (Unless of course, it's a protected function and you extend the class).

    0 讨论(0)
提交回复
热议问题