Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with emails

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?

like image 215
Jeff Davidson Avatar asked Mar 27 '26 01:03

Jeff Davidson


1 Answers

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 `/email

also 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.

like image 118
Jakub Avatar answered Mar 29 '26 20:03

Jakub