Welcome Guest, Not a member yet? Register   Sign In
Generating System emails
#1

[eluser]frist44[/eluser]
I have a social app where I'm generating emails for registration, upload of content, comments, etc.

I have each email subject, body, and associated keyword ("REGISTRATION") in a database. I have a model that grabs stuff from there, and then substitutes information like name, url, and other links a particular email may need. However, it just seems like I have to go through a lot of steps to add another email (add to database, add function to model, call it in the controllers...).

How are you guys handling these?
#2

[eluser]frist44[/eluser]
No one sends emails from their application?
#3

[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
}




Theme © iAndrew 2016 - Forum software by © MyBB