[eluser]mddd[/eluser]
I like to put my emails into views. I make a folder 'emails' inside the view folder for that.
Often, emails are sent at the same time as saving changes to the database. For instance, an application that allows a user to save something to a calendar.
There could be a Calendar model that takes care of those changes. Then you get something like this:
Code:
class Calendar extends Model
{
function add_event($data)
{
// add event to the calendar
// ....
// send email to the user
$user_data = $this->user_model->get_user_info($data['user_id']);
// now we have some user data and some calendar data (which already was here because we were adding stuff to the calendar)
$message = $this->load->view('emails/event_added', compact('data', 'user_data'), true);
$this->email->to($user_data['email_address']);
$this->email->subject('Your event was added');
$this->email->message($message);
$this->email->send();
}
}
If every email is going to a registered user, you could make it a bit shorter by adding a small email-sending library to take care of recurring actions:
Code:
class Calendar extends Model
{
function add_event($data)
{
// add event to the calendar
// ....
// send email to the user
$user_data = $this->user_model->get_user_info($data['user_id']);
// now we have some user data and some calendar data (which already was here because we were adding stuff to the calendar)
// use email-sending library
$this->email_sender->send('event_added', $user_data['id'], $data);
}
}
In this case, the email-sending library would contain a method like this:
Code:
function send($email_type, $user_id, $data)
{
// get some user info from the database to use in the email (like address, the user's name etc).
// set the title based on $email_type and load the correct view, filled with data from $data and from the user info
}