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
|
|
|
|
{
|
2014-03-16 12:46:16 +08:00
|
|
|
public $name;
|
|
|
|
public $email;
|
|
|
|
public $subject;
|
|
|
|
public $body;
|
|
|
|
public $verifyCode;
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2016-04-29 00:06:20 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
|
|
|
* @return array the validation rules.
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// name, email, subject and body are required
|
|
|
|
[['name', 'email', 'subject', 'body'], 'required'],
|
|
|
|
// email has to be a valid email address
|
|
|
|
['email', 'email'],
|
|
|
|
// verifyCode needs to be entered correctly
|
|
|
|
['verifyCode', 'captcha'],
|
|
|
|
];
|
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
|
|
|
* @return array customized attribute labels
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'verifyCode' => 'Verification Code',
|
|
|
|
];
|
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
/**
|
|
|
|
* Sends an email to the specified email address using the information collected by this model.
|
2016-04-29 00:06:20 +08:00
|
|
|
* @param string $email the target email address
|
2014-03-16 12:46:16 +08:00
|
|
|
* @return boolean whether the model passes validation
|
|
|
|
*/
|
|
|
|
public function contact($email)
|
|
|
|
{
|
|
|
|
if ($this->validate()) {
|
2014-06-26 22:28:28 +08:00
|
|
|
Yii::$app->mailer->compose()
|
2014-03-16 12:46:16 +08:00
|
|
|
->setTo($email)
|
|
|
|
->setFrom([$this->email => $this->name])
|
|
|
|
->setSubject($this->subject)
|
|
|
|
->setTextBody($this->body)
|
|
|
|
->send();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-05 22:41:11 +08:00
|
|
|
return false;
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
2013-05-24 22:14:49 +08:00
|
|
|
}
|