Reputation: 1929
I'm curious to know how to accomplish this task.
I have a function in my library that will send out an email. However it takes a parameter of $type and the type could be forgot password, successful registration, etc.
I want to be able to set parts of that type such as the subject of it as well as the message to be displayed for the different types of emails in one file to make it easier to edit down the line.
What kind of file should I create inside of codeigniter to accomplish this?
Upvotes: 3
Views: 179
Reputation: 20475
Jeff,
Take a look at Tank Auth, they handle this sort of email thing already (its a small CI plugin/library for authentication, email password recovery, etc).
Just go through the code and pull out the email template you want, it comes in plain text and HTML flavor.
Proably the best way to always learn is to review other code.
Example from the auth.php controller (for reference):
/**
* Send email message of given type (activate, forgot_password, etc.)
*
* @param string
* @param string
* @param array
* @return void
*/
function _send_email($type, $email, &$data)
{
$this->load->library('email');
$this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
$this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
$this->email->to($email);
$this->email->subject(sprintf($this->lang->line('auth_subject_'.$type), $this->config->item('website_name', 'tank_auth')));
$this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));
$this->email->set_alt_message($this->load->view('email/'.$type.'-txt', $data, TRUE));
$this->email->send();
}
Tank Auth is a good library, and easy to work with.
EDIT
a view simply means generated output to a user, wether it is a EMAIL or an HTML browser returned page doesn't matter. the "text" you speak of is a file reference
$type.'-txt'
is calling say$type="hello";
==>hello-txt
page in your view folder `/emailalso the config references are because tank auth has its own config file in your
/apps/config/
folder the script references it for things like from and reply to, also you see the multi-lingual language support in$this->lang->line('auth_subject_'.$type)
which proably is not needed for you.
Upvotes: 4
Reputation: 6895
The easiest way to do it is to create a config file for your mailer with the subjects and bodies of your email.
Load this config file in your mailing function.
The codeigniter user guide about config files has all the details about how to create config files: http://codeigniter.com/user_guide/libraries/config.html
In a nutshell,
In that file add a config array with the stuff you need, for example
$config['templates']['lost_password']['subject'] = 'Lost Password';
$config['templates']['lost_password']['body'] = 'The body';
[...]
$this->config->load('mail_template', true);
$ templates = $this->config->item('templates', 'mail_template');
Upvotes: 1
Reputation: 7902
Why not create a series of views for each type (say in a folder called 'email_templates'), with variables where appropiate. You could then use the view function to get the html, passing it into your email function (note third parameters that returns the html instead of rendering it.
$html = $this->load->view('email_templates/forgot_password', $data, TRUE);
Upvotes: 1