Yii 1.1: Email template functionality with manage content from database

13 followers

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,

database

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

Capture2.jpg

Quick view to Front side

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

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

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

if(isset($_POST['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;
    $mailer->Send();

Plz comment for any query or updation.

Total 5 comments

#17765 report it
Ankit Modi at 2014/07/21 12:37am
suggestion

Hi I have noticed that you can repalce the every varible or I think your way is very long but not bad

$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

Just simple and easy way you can replace the value using key

$message = $dbTemplateModel->content;
$variables=array(
    '##MESSAGE##'=>$model->message,
    '##USERNAME##'=>$model->name,
    '##USEREMAIL##'=>$model->email,
 
);
$final_array = $this->EmailSendArray($message , $variables);//replace the variables
 
 public function EmailSendArray($message,$variables){
        $searchArray=array_keys($variables);
        $replaceArray=array_values($variables);
        return str_replace($searchArray,$replaceArray,$message);
    }

I hope it's help someone,

more details please click here...

#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

{firstname}
first I instatiate the emailTemplate model
$email=emailTemplate::model->find(.....)
//then I load the relevant model, in my case a customer model
$customer=Customer::model()->findByPK($customerID);
//of course, you can always load other models, in this case through a relation
$emails=$customer->emails
//and then I use a function in the emailTemplate model
$email->insertFields($customer);
$email->insertFields($emails);
....

which looks like this

public function insertFields($data) {
            foreach($data as $k=>$field) {
               $j="{".$k."}";
               $this->body=str_replace($j,$field,$this->body);
            }
            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 */
));
$emailReset->send();
#14476 report it
trond at 2013/08/15 02:57am
Neat

Very interesting solution. Thanks for sharing.

Leave a comment

Please to leave your comment.

Write new article