Email template functionality with manage content from database

You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#2)

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.