You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
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%">
<tr>
<td>
<?php
$path = $_SERVER['HTTP_HOST'];
?>
<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.
Neat
Very interesting solution. Thanks for sharing.
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();
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 $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; }
@kiran sharma
Good work :-)
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...
good
Great solution, we can use it.
Thanks
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.