here is my code.
ContactForm.php
<?php
/**
* ContactForm class.
* ContactForm is the data structure for keeping
* contact form data. It is used by the 'contact' action of 'SiteController'.
*/
class ContactForm extends CFormModel
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
and contact action is
public function actionContact() {
$model = new ContactForm;
if (isset($_POST['ContactForm'])) {
$model->attributes = $_POST['ContactForm'];
if ($model->validate()) {
$name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
$subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
$headers = "From: $name <{$model->email}>\r\n" .
"Reply-To: {$model->email}\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/plain; charset=UTF-8";
mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact', array('model' => $model));
}
and contact form is
<?php if (Yii::app()->user->hasFlash('contact')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('contact'); ?>
</div>
<?php else: ?>
<?php $form = $this->beginWidget('CActiveForm'); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php // echo $form->errorSummary($model); ?>
<div class="input-group">
<div class="input-group-addon primary-color">
<i class="fa fa-user"></i>
</div>
<?php echo $form->textField($model, 'name', array('placeholder' => 'Name', 'class' => 'form-control')); ?>
</div>
<div class="input-group">
<div class="input-group-addon primary-color">
<i class="fa fa-envelope"></i>
</div>
<?php echo $form->textField($model, 'email', array('placeholder' => 'Email', 'class' => 'form-control')); ?>
</div>
<div class="input-group">
<div class="input-group-addon primary-color">
<i class="fa fa-pencil"></i>
</div>
<?php echo $form->textField($model, 'subject', array('placeholder' => 'Subject', 'class' => 'form-control', 'size' => 60, 'maxlength' => 128)); ?>
</div>
<?php echo $form->textArea($model, 'body', array('placeholder' => 'Message', 'class' => 'form-control', 'rows' => 6, 'cols' => 50)); ?>
<?php if (CCaptcha::checkRequirements()): ?>
<div class="input-group">
<!-- <div class="input-group-addon primary-color">
<i class="fa fa-search"></i>
</div>-->
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model, 'verifyCode', array('class' => 'form-control', 'placeholder' => 'Please enter Captcha')); ?>
<?php endif; ?>
</div>
<br/>
<?php echo CHtml::submitButton('Submit', array('class' => 'btn btn-primary')); ?>
<?php $this->endWidget(); ?>
</div>
<?php endif; ?>