1

I am able to send simple email to receiver.Now i have to be able to select a email format from existing different formats and this template must appear in my message box.i should also be able to change the content of the template that appears in message box before sending to receiver.How to do this?

My code for view form

<div class="form wide">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'mail-form',
'enableAjaxValidation'=>true,
'htmlOptions' => array('enctype' => 'multipart/form-data'), // ADD THIS
)); ?>
<div class="row col2">
    <?php echo $form->labelEx($model,'email_from'); ?>
    <?php echo $form->textField($model,'email_from',array('size'=>50,'maxlength'=>50,'readonly'=>'readonly')); ?>
    <?php echo $form->error($model,'email_from'); ?>
</div>
<div class="row col2">
    <?php echo $form->labelEx($model,'email_to'); ?>
    <?php echo $form->textField($model,'email_to',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'email_to'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
    <?php echo $form->labelEx($model,'subject'); ?>
    <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>250)); ?>
    <?php echo $form->error($model,'subject'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
    <?php echo $form->labelEx($model,'message'); ?>
    <?php echo $form->textArea($model,'message',array('style'=>'width: 680px; height: 300px;')); ?>
    <?php echo $form->error($model,'message'); ?>
</div>
<div style="clear:both"></div>
<div class="row buttons">
    <?php
    echo CHtml::submitButton($model->isNewRecord ? 'Send' : 'Send',array('class' => 'btn'));
     ?>
</div>

<?php $this->endWidget(); ?>
</div><!-- form -->
Ram lamsal
  • 87
  • 8
  • you should use any html editor to to show the template with html rendered design and to edit the same with wysiwyg(What You See Is What You Get) – Ahmad Asjad Feb 01 '16 at 10:59
  • you can use any html editor. Free html editors are also available in the market for the same purpose. http://ckeditor.com/demo – Ahmad Asjad Feb 01 '16 at 11:01
  • I have said that i am already able to send message .Now is another case.i have different message format suppose in one format there may be message like 'Happy new year'.in other format there may be 'thank you for cooperation ',and other message format.if i select any format i have to display that message format in my form's message box. – Ram lamsal Feb 01 '16 at 11:18
  • Please check my answer. – Ahmad Asjad Feb 01 '16 at 11:28

2 Answers2

0

Try below code:

public function actionContact() {
    $model = new ContactForm();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {

        $status = Yii::$app->mailer->compose()
            ->setTo($model->email_to)
            ->setFrom([$model->email_from => $model->email_from])
            ->setSubject($model->subject)
            ->setTextBody($model->message)
            ->send();

        if ($status) {
            Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
        } else {
            Yii::$app->session->setFlash('error', 'There was an error sending email.');
        }
        return $this->refresh();
    } else {
        return $this->render('contact', [
            'model' => $model,
        ]);
    }
}

You can also create a email_template directory under views folder and create template file in which you can set email template content.

thepaks
  • 203
  • 1
  • 7
0

you can use js or ajax to do the same. 1: Use jQuery: I assume that you are using jQuery. So use ajax.

.......JS Cod........
/*I assume that you have your template data in an attribute like data-template-data=""*/
$('#select_template_id').chang(function(){
    template=$(this).attr('data-template-data');
    $('#template_edit_box').val(template);
});
Ahmad Asjad
  • 825
  • 1
  • 8
  • 29
  • i have just made separate .php file for different format and don't know how to do this .can you expalin in detail. – Ram lamsal Feb 02 '16 at 04:29