Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter put email in a global function

I can send an email from within a controller with the example in the CodeIgniter documentation. I was wondering how I would put the email code on a global functions page so I have access to it all over with a simple function call.

// in controller

emailTest($to, $subject, $message);

// on a global functions page

function emailTest($to, $subject, $message) {

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

    $this->email->from('[email protected]', 'My Name');
    $this->email->to($to); 
    $this->email->subject($subject);
    $this->email->message($message);    

    $this->email->send();
}
like image 843
user1738750 Avatar asked Apr 15 '26 22:04

user1738750


1 Answers

You can create a helper and just load it up when you need it.

$this->load->helper('email');
send_email($to, $subject, $message);

Edit: Since you want to use the inbuilt email functionality, libraries would be a better bet:

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

class MY_Library {

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

    public function send_email($to, $subject, $msg)
    {
        $this->CI->load->library('email');

        $this->CI->email->from('[email protected]', 'My Name');
        $this->CI->email->to($to); 
        $this->CI->email->subject($subject);
        $this->CI->email->message($message);    

        $this->CI->email->send();
    }

}

Then you can call it like so:

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

$this->my_library->send_email('[email protected]', 'RE: test message','cool message');
like image 140
stormdrain Avatar answered Apr 20 '26 07:04

stormdrain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!