2013-05-24 22:14:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\models;
|
|
|
|
|
2013-11-08 12:01:13 +08:00
|
|
|
use Yii;
|
2013-05-24 22:14:49 +08:00
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContactForm is the model behind the contact form.
|
|
|
|
*/
|
|
|
|
class ContactForm extends Model
|
|
|
|
{
|
|
|
|
public $name;
|
|
|
|
public $email;
|
|
|
|
public $subject;
|
|
|
|
public $body;
|
|
|
|
public $verifyCode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array the validation rules.
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2013-10-18 17:48:27 +08:00
|
|
|
return [
|
2013-05-24 22:14:49 +08:00
|
|
|
// name, email, subject and body are required
|
2013-11-12 22:39:23 +08:00
|
|
|
[['name', 'email', 'subject', 'body'], 'required'],
|
2013-05-24 22:14:49 +08:00
|
|
|
// email has to be a valid email address
|
2013-11-13 17:41:25 +08:00
|
|
|
['email', 'email'],
|
2013-05-24 22:14:49 +08:00
|
|
|
// verifyCode needs to be entered correctly
|
2013-11-13 17:41:25 +08:00
|
|
|
['verifyCode', 'captcha'],
|
2013-10-18 17:48:27 +08:00
|
|
|
];
|
2013-05-24 22:14:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array customized attribute labels
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
2013-10-18 17:48:27 +08:00
|
|
|
return [
|
2013-05-24 22:14:49 +08:00
|
|
|
'verifyCode' => 'Verification Code',
|
2013-10-18 17:48:27 +08:00
|
|
|
];
|
2013-05-24 22:14:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an email to the specified email address using the information collected by this model.
|
|
|
|
* @param string $email the target email address
|
|
|
|
* @return boolean whether the model passes validation
|
|
|
|
*/
|
|
|
|
public function contact($email)
|
|
|
|
{
|
|
|
|
if ($this->validate()) {
|
2013-11-08 12:01:13 +08:00
|
|
|
Yii::$app->mail->compose()
|
|
|
|
->setTo($email)
|
|
|
|
->setFrom([$this->email => $this->name])
|
|
|
|
->setSubject($this->subject)
|
|
|
|
->setTextBody($this->body)
|
|
|
|
->send();
|
2013-05-24 22:14:49 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|