Code-Igniter: load & send email in a library

前端 未结 2 1723
礼貌的吻别
礼貌的吻别 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(...)

提交回复
热议问题