Yii 1.1: Email template functionality with manage content from database


Quick view to Admin side

I hadn't specify whole flow as I assume you know yii crud flow well, and Quick understand of my specified admin flow.. :)

I use Mailer Extension for email.

Create table to store email template content as below,


Make pre-define entry in database, so admin can edit particular template contant with specific position of replace variables.. Edit page shown as below.


Quick view to Front side

Create template file any where in front side (i.e template/templatecontact) as below,

<table style="width: 100%">
        <a href="<?php echo $_SERVER['HTTP_HOST']; ?>">
                    <img src="<?php echo $_SERVER['HTTP_HOST']; ?>/img/logo.png"/> <!-- This is site logo image -->
        <table style="border: solid 2px; border-color: #ADCE97; width: 100%">
        <div style="margin: 5px 0px 5px 0px;">
            ##CONTENT## <!-- This will be replaced with final $message data -->

Below code is controller where your template parameters are post (i.e site/contact)

    $model->attributes=$_POST['Contact']; // Set this if you save email data OR use direct $_POST data
    $adminEmail = "AdminEmail [AT] xyzcompanyadminemail . com"; //Receiver
    $dbTemplateModel = Template::model()->findByAttributes(array('key'=>'CONTACT-REQ-USER')); // From here you can get template content set by admin CONTACT-REQ-USER is key in db
    $message = $dbTemplateModel->content;
    $message = str_replace("##MESSAGE##", nl2br($model->message), $message); //Replace ##MESSAGE## by user send message
    $message = str_replace("##USERNAME##", $model->name, $message);
    $message = str_replace("##USEREMAIL##", $model->email, $message);
    $emailTemplate = $this->renderPartial('template/templatecontact',array(),true); //Getting email template content
    $message = str_replace("##CONTENT##", $message, $emailTemplate); //Replacing ##CONTENT## in email template by $message data
    $mailer = Yii::createComponent('application.extensions.mailer.EMailer');
    $mailer->IsHTML(true); //HTML format
    $mailer->From = $model->email; //Sender
    $mailer->AddReplyTo($model->email); //Sender
    $mailer->AddAddress($adminEmail); //Receiver
    $mailer->FromName = $model->name;
    $mailer->CharSet = 'UTF-8';
    $mailer->Subject = Yii::t('contact-req', $model->subject);
    $mailer->Body = $message;

Plz comment for any query or updation.

Total 4 comments

#14628 report it
Rajith R at 2013/08/30 01:58am
@kiran sharma

Good work :-)

#14487 report it
Chris Backhouse at 2013/08/16 10:57am
Generic substitution

I've inverted the substitution which allows any field name in the models being read, thus in the template I can add

first I instatiate the emailTemplate model
//then I load the relevant model, in my case a customer model
//of course, you can always load other models, in this case through a relation
//and then I use a function in the emailTemplate model

which looks like this

public function insertFields($data) {
            foreach($data as $k=>$field) {
            return true;
#14478 report it
logity at 2013/08/15 09:28am
Put send functionality into generic class

I used similar approach http://radzserg.com/2013/06/06/how-do-i-send-emails-in-my-applications/ But also put that functionality in classes. I.e. the final result should look like this

$emailReset = new \MyApp\email\ResetPassword(array(
    'user' => $userModel,
    'hash' => $hash,
    /* other dynamic data for email */
#14476 report it
trond at 2013/08/15 02:57am

Very interesting solution. Thanks for sharing.

Leave a comment

Please to leave your comment.

Write new article