Welcome Guest, Not a member yet? Register   Sign In
Sending email from Model
#1

[eluser]bewhite[/eluser]
Hi!

I have problem with email. When I have tried to use it in the Controller then everything is ok. Now I have decided to separate all email related functions to the special class Mailer. Here is code:
Code:
class Mailer extends Model
{
  public

  function __construct()
  {
    parent::Model();
    $this->load->library('email');
  }

  function subscribe($email)
  {
    $subscriber = array('email' => $email);
    
    $this->db->insert('subscribers', $subscriber);
  }
  
  function mail_action($name, $email, $login, $password)
  {
    $initial = array('[FIO]', '[LOGIN]', '[PASSWORD]');
    $final = array($name, $login, $password);
  
    $template = read_file('system/application/views/mails/crew.php');
    
    $this->email->to($email);
    $this->email->from('[email protected]');
    
    $this->email->subject('Welcome to the mysite.com!');
    $this->email->message(str_replace($initial, $final, $template));
    
    $this->email->send();
  }
}
When I'm trying to call mail_action then I recieve Internal Server Error. Maybe it happens because I have used User Guide example of code and there is line with words 'assuming you are using Controller during email sending'. Can anybody show example of email sending from Model? Or just take a look at my code and tell me what should I change to make it workable?

Victor
#2

[eluser]xwero[/eluser]
Why do you want the class to be a child of the model class?

I think if you want to create a separate mailer class you are better of with a class in the libraries directory.
Code:
class Mailer
{
   var $ci;

   function Mailer()
   {
     $this->ci =& get_instance();
     $this->ci->load->library('email');
     $this->ci->load->database(); // if you didn't autoload it
   }
  
   function new_subscriber($id)
   {
      $this->ci->load->model('Usermodel','',true);
      list($name, $email, $login, $password) = $this->ci->Usermodel->mail_new_subscriber($id);
      // your code
      $initial = array('[FIO]', '[LOGIN]', '[PASSWORD]');
      $final = array($name, $login, $password);
  
      $template = read_file('system/application/views/mails/crew.php');
    
      $this->ci->email->to($email);
      $this->ci->email->from('[email protected]');
    
      $this->ci->email->subject('Welcome to the mysite.com!');
      $this->ci->email->message(str_replace($initial, $final, $template));
    
      $this->ci->email->send();
   }
   // other mail methods
}
To use this library you have to do
Code:
//model
function new_subscriber()
{
    // insert query
    return $this->db->insert_id();
}

function mail_new_subscriber($id)
{
   // select query
   return $query->row_array();
}
// controller
function newsletter()
{
  // load model and library
  if($validation == 'ok')
  {
    $id = $this->Usermodel->new_subscriber();
    $this->mailer->new_subscriber($id);
  }
}
This way you can centralize all email actions which is easier than keeping your email actions in your models, even if you are making modules it's better this way.
#3

[eluser]Michael Ekoka[/eluser]
I second what xwero said. Model should be used for items such as database access and to a certain extent, validation. Emails are accessories to a web app and should go in libraries or plugins.




Theme © iAndrew 2016 - Forum software by © MyBB