The more I use it, the more and more I love CodeIgniter as a framework; in fact I believe I cherish it more than CakePHP due to it’s simplicity. One thing that I do like about CakePHP is that by mixing in PHPMailer you get the ability to specify views for your html email and [...]
The more I use it, the more and more I love CodeIgniter as a framework; in fact I believe I cherish it more than CakePHP due to it’s simplicity. One thing that I do like about CakePHP is that by mixing in PHPMailer you get the ability to specify views for your html email and views for your text emails. So you could have multipart emails being sent based off the data in these views.
Sadly, this is one thing that CodeIgniter is lacking. But if you look around the docs long enough (which are absolutely fantastic by the way) you’ll see that you can use CodeIgniter’s built in template parsing class to do the same thing.
Let’s assume that we are building a site that allows users to create an account. When they create an account, they need to get a welcome email from the application. I’m going to assume you know how to build the form and process the data so here’s the controller action that handles the mail part of things.
function signup(){ #stripped out the validation code #stripped out the db insert code $data = array( 'some_var_for_view'=>'Some Value for View' ); $htmlMessage = $this->parser->parse('user/email/signup_html', $data, true); $txtMessage = $this->parser->parse('user/email/signup_txt', $data, true); #send the message $this->email->from('', 'CSSNinja'); $this->email->to($this->input->post('email_address')); $this->email->subject('Account Registration Confirmation'); $this->email->message($htmlMessage); $this->email->alt_message($txtMessage); $this->email->send(); }
Nothing really ground breaking going on here, but notice the two lines that declare the variables $htmlMessage and $txtMessage, these two lines are what’s going to allow us to use these views as email templates.
The template parsing class will parse out a template using the following syntax:
$this->parser->parse('path/to/view',$data_array);
However, if you pass a true value as an optional third parameter (default is false) CodeIgniter does not render the view, it returns its fully parsed text as a string. So the $htmlMessage variables and $txtMessage variables now hold appropriately formatted code for either a text message or an html message which is exactly what CodeIgniter’s email function needs for its message() and alt_message() methods.
Hope this was helpful, post any questions, concerns or moral outrages in the comments.
15 Responses
This is the method we use to pull in data from a file that we are using to template a page. It doesn’t add the overhead of templating and allows us to create slightly more general templates—there really is nothing that comes close to having your basic control structures in a template in my opinion. In your use, we can even create a very basic html email “template” that subsequently uses this same method to include the content of the particular emails using a switch statement.
Have you yet checked into Kohana? It is a project forked off CodeIgniter that adopted a community development model to fix some of the known issues with CodeIgniter that simply aren’t being dealt with. We used Kohana for the project we’re working on in the studio right now. It is very close ot what we want in a framework, but now having built an entire site in it, there are some glaring weaknesses we found. Our biggest complaints were with its (lack of) extensibility to create a system of dropping in modules, and leaving much to be desired with form handling and validation.
And, having not seen something else that we like as much, we’re now building our own.
Brilliant tip. Thanks for drawing my attention to this feature!
This is great stuff! So ingenious to use the Codeigniter parser… Thanks for posting this.
Great explanation. Thanks.
Probably is also good to say that Parser is a CodeIgniter Library, and of course you need to load it before using.
You can easily load it using:
$this->load->library('parser');
Not sure why the code for loading the library was not published in my previous comment, but I wrote:
$this->load->library(‘parser’);
Thank you for your help.
Saved me 2 hours of figuring out what’s happening or how I can make it work
Hello, your stuff is really cool. I mostly agree with you and would like to see more emotions here.Thanks and see you soon on your blog.:)
proffesional service is a must
Awesome,
Thanks dude, I’ve been wondering how to for this for a while now, it wasn’t immediately obvious from the CI guide.
Goog tip!
Just my 2 cents: for alternative text message $this->email->set_alt_message.
iñ
I have try your tutorial, here is some portion of my code in my controller:
$config['protocol'] = ‘sendmail’;
$config['mailtype'] = ‘html’;
$config['charset'] = ‘utf-8′;
$config['wordwrap'] = TRUE;
$data = array(
‘email_subject’=>’Comment Information’,
‘profile_first_name’=>$your_profile->profile_first_name,
‘profile_last_name’=>$your_profile->profile_last_name,
‘friend_name’=>$this->input->post(‘friend_name’),
‘username’=>$this->session->userdata(‘username’),
‘comment_text’=>$this->input->post(‘comment_text’)
);
$this->load->library(‘parser’);
$htmlMessage = $this->parser->parse(‘email/html_template_for_comment_email’, $data, true);
$this->email->initialize($config);
$this->email->from(‘’, ‘PersonaFlag: A Location-based Social Networking’);
$this->email->to($friend_profile->profile_email);
$this->email->subject(‘Comment Confirmation’);
$this->email->message($htmlMessage);
$this->email->send();
and here are my xhtml/php code for the email template:
Dear
commented on your status.
wrote:
the email was sent, but is only the html text, the variable like $email_subject, etc… empty…
please help me…
Maybe you could change the blog name title Email Templates with CodeIgniter | WebDevKungfu to something more suited for your subject you make. I enjoyed the the writing withal.
$txtMessage = $this->parser->parse(‘user/email/signup_txt’, $data, true);
What in *.txt file?
Can somebody explains how can i send emails from Codeigniter using the phpmail() function instead of using an smtp host ?
I getting error, when i enter html in email script…. It display as is it in the email…