Question

How can I create a helper class in codeigniter to store all email which sends and receives in my website. I need to call that class with all email functions

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

If I call this class then this function should store $to,time, body and subject of the email to a table (table1). How it is possible?

Was it helpful?

Solution

Create a library with the name of "myemail" and place that in application/libraries.

application/libraries/myemail.php

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

class CI_Myemail
{
    public function __construct()
    {      
        $this->CI =& get_instance();
    }
        public function saveEmail($to,$body,$subject)
        {
             $this->CI->load->model("table_model");
             $this->table_model->save(array("to"=>$to,"body"=>$body,"subject"=>$subject,"mail_sent_time"=>date("Y-m-d H:i:s")));
        }
}

Then you have to create table_model and write function to save the data into data.

In controller, you have to load this library as

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

In controller, you have to call as

$this->myemail->saveEmail($this->to,$body,$subject);// Here, $this->do is controller variable as you mentioned in comment

OTHER TIPS

You can easily do this by having a helper function to send an email. Where ever you need to send the email call that function. Inside the function save the data to a table before calling the method to send the email. Also you can save the emails without sending with status pending and send it by a cronjob to improve user experience. I am doing same thing in my website.

The helper function as I am using it below. You can tune it for your needs. The data array should have all the details when called from a controller.

function sendEmail($data, $immediate=FALSE) {
$subject = $data['subject'];
$to = $data['to'];
$viewName = $data['template'];
$CI = & get_instance();

$CI->config->load("thephpcode.com");
$from = $CI->config->item('Sender');
$fromName = $CI->config->item('SenderName');
$priority = $CI->config->item('Priority');

if (isset($data['from'])) {
    $from = $data['from'];
    $fromName = $data['fromName'];
}

if (isset($data['priority']))
    $priority = $data['priority'];

$body = $CI->load->view($viewName, $data, TRUE);

if ($from == "") {
    log_message('error', 'From value is not set in Email helper for sending email');
    return;
}
$bcc = '';
if (isset($data['bcc'])) {
    $bcc = $data['bcc'];
}
/*
$replyto ='';
if (isset($data['reply_to'])) {
    $replyto = $data['reply_to'];
    $replytoname = $data['reply_to_name'];
}
*/
$status = 'Pending';
if ($immediate)
{
    $status = 'Sent';
}
$dbdata = array();
$dbdata['from'] = $from;
$dbdata['fromName'] = $fromName;
if (isset($data['reply_to'])) {
    $dbdata['replyto'] = $data['reply_to'];
    $dbdata['replytoname'] = $data['reply_to_name'];
}
$dbdata['to'] = $to;
$dbdata['subject'] = $subject;
$dbdata['body'] = $body;
$dbdata['bcc'] = $bcc;
$dbdata['status'] = $status;
$dbdata['priority'] = $priority;
$CI->load->model('email_model', 'email_model');
$CI->email_model->insert($dbdata, 'email_queue');

if (!$immediate)
{
    return TRUE;
}
//Send the email    
$CI->load->library('email');
$CI->email->initialize($CI->config->item('email_config'));

$CI->email->from($from, $fromName);
$CI->email->to($to);
if (isset($data['bcc']))
{
    $CI->email->bcc($data['bcc']);
}
if (isset($data['reply_to']))
{
    $CI->email->reply_to($data['reply_to'],$data['reply_to_name']);
}
$CI->email->subject($subject);
$CI->email->message($body);
$CI->email->send();

return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top