[eluser]Unknown[/eluser]
HI all,
I'm building a newsletter module and i need you advice
Basically, newsletter's templates are selectables and edtable by the administrator with ckeditor.
Newsletters and Templates are saved in a mysql table as html. Elements like {name}, {site_url}, {unsubscrib} {account} ...etc are parsed with the codeigniter template parser. But in this situaltion, the limitation of template parse class is it needs a view file, so not really 'out of the box' for html saved in a database.
This is how i've managed to get it working:
The controller who gets subscribers form model, and send mails:
Code:
// this function gets te whole newsletter object (subject, body...etc) and specfics data to parse
function _parse_newsletter($newsletter, $parsers = array())
{
$this->load->library('parser');
// default values
$parse = array(
'site_name' => $this->common->meta('site_name'),
'site_url' => site_url(),
'subject' => $newsletter->nl_subject
);
if (is_array($parsers) && count($parsers) > 0)
{
$parse = $parsers + $parse;
}
// template is a class (controller) property accessible from view file
$this->template = $newsletter->nl_body;
return $this->parser->parse('newsletter/newsletter_view', $parse, true);
}
// this is not the whole function... too long to post here
// we loops throught subscribers and assign individuals values, then send mails
function send_newsletter()
{
//... getting $newsletter and $subscribers batch before loop...
foreach ($subscribers as $subscriber)
{
$data['account']= site_url('newsletters/newsletter_account/'.$subscriber->nl_hash);
$data['name'] = $subscriber->nl_name;
$data['email'] = $subscriber->nl_email;
$data['view_online'] = site_url('newsletters/online/subscriber/'.$subscriber->nl_hash.'/id/'.$newsletter->nl_hash);
$newsletter_body = $this->_parse_newsletter($newsletter, $data);
$this->email->clear();
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$this->email->initialize($config);
$this->email->to($subscriber->nl_email);
$this->email->from('[email protected]');
$this->email->subject($newsletter->nl_subject);
$this->email->message($newsletter_body);
$email_sent = $this->email->send();
}
}
finally, in the view file (newsletter_view.php) called by _parse_newsletter function, i simply echo $this->template property:
Code:
<?php
echo $this->template;
?>
Do you think we can do this in a simpliest way? i prefer use the native codeigniter class instead of writing new ones, and it's the 'best' way i found to send personnalized mails not templated in a php file. But maybe i'm on the wrong path...