Difference between #2 and #1 of Email template functionality with manage content from database

unchanged
Title
Email template functionality with manage content from database
unchanged
Category
Tutorials
unchanged
Tags
email, template
changed
Content
### 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](http://www.yiiframework.com/extension/mailer/
"Mailer") for email.

Create table to store email template content as below,

![database](http://i1147.photobucket.com/albums/o556/master753951/Capture.jpg
"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.

<img
src="http://i1147.photobucket.com/albums/o556/master753951/Capture2.jpg"
width="600" />

### Quick view to Front side

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



~~~
[php]
<table style="width: 100%">
<tr>
		<td>
		<?php
			$path = $_SERVER['HTTP_HOST'];
		?>
		<a		<a href="<?php echo $_SERVER['HTTP_HOST'];
?>"><img?>">
                    <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)



~~~
[php]
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.**