In this tutorial, you will learn how to create a custom library in CodeIgniter 3, sometime in your application you might require to create a custom library, so we can create a custom library and we can quickly load the library in our controller and call the library function.
In this tutorial, we will create the Email template library. In our application, we send multiple emails to the users for example, when users create an account we send an account verification email or when the user reset password we send password reset email. using the custom library we will handle the email template. Just follow the below steps to create your own library for your application.

In the CodeIgniter framework inside the application folder, there is a libraries folder, where you can create a custom library.

Create the Email_template.php library inside application/libraries folder

Email_template.php
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Email_template {

    private $ci;

    public function __construct() {
        $this->ci = & get_instance();
    }

    function _create($type, $data) {
        switch ($type) {
            case 'forgot_password':
                $to_email = $data['to_mail'];
                $subject = "Reset your Example.com password";
                $message = $data['message'];
                $cc = "";
                $this->_send_email($to_email, $subject, $message, 'no-reply@example.com', $cc, $this->reply_to);
                break;
            case 'user_registration':
                $to_email = $data['to_mail'];
                $subject = "Verify your Example.com account";
                $message = $data['message'];
                $cc = array();
                $this->_send_email($to_email, $subject, $message, 'no-reply@example.com', $cc, $this->reply_to);
                break;
            default :
                $subject = '';
                $message = '';
        }
    }

    private function _send_email($to_email, $subject, $message, $from_email = "info@example.com", $cc = "", $reply_to = "", $attach = array()) {
        $this->ci->load->library('email');
        $this->ci->email->set_newline("\r\n");
        $this->ci->email->from($from_email, "Example");
        $this->ci->email->to($to_email);
        if (!empty($cc)) {
            $this->ci->email->cc($cc);
        }
        if (!empty($reply_to)) {
            $this->ci->email->reply_to($reply_to);
        }
        if ($this->show_bcc) {
            $this->ci->email->bcc($this->bcc);
        }
        $this->ci->email->subject($subject);
        $this->ci->email->message($message);
        if (!empty($attach)) {
            foreach ($attach as $att) {
                $this->ci->email->attach($att);
            }
        }
        $send = $this->ci->email->send();
        if ($send) {
            return true;
        } else {
            return false;
        }
    }

}

?>

How to load the custom library in controller
We have created the custom library, we need to load that library inside the controller in order to use it. 

User_Controller.php
<?php

header('Access-Control-Allow-Origin: *');
defined('BASEPATH') OR exit('No direct script access allowed');

class User_Controller extends CI_Controller {

    public $data;
    public $user_id;

    function __construct() {
        parent::__construct();
        $this->load->model("home_m");
        $this->user_id = $this->session->userdata('uid');
        $this->load->library('Email_template'); //Load Email template library
    }

    public function register() {
        //Write logic to register user, then use the Email_template library to send the email to users.        
        $data['to_mail'] = $this->input->post('user_email');
        $this->data['message'] = $this->load->view('email_templates/register_tpl', $this->data, true);
        $type = "user_registration";
        $this->email_template->_create($type, $this->data);
    }

}
?>

We have loaded the Email_template library in User_Controller __construct() function, so we can use it anywhere in the controller. 

Written by
Check out this Author's contributed articles.
If you like Tutorialsmint and would like to contribute, you can also write an article using Write an Article. See your article appearing on the Tutorialsmint main page and help other programmers to enhance their knowledge.

Comments(0)

RECENT TUTORIALS
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Edit Post
Add Reply