yiisoft /
yii2-app-basic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\base\Model; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * ContactForm is the model behind the contact form. |
||
| 10 | */ |
||
| 11 | class ContactForm extends Model |
||
| 12 | { |
||
| 13 | public $name; |
||
| 14 | public $email; |
||
| 15 | public $subject; |
||
| 16 | public $body; |
||
| 17 | public $verifyCode; |
||
| 18 | |||
| 19 | |||
| 20 | /** |
||
| 21 | * @return array the validation rules. |
||
| 22 | */ |
||
| 23 | public function rules() |
||
| 24 | { |
||
| 25 | return [ |
||
| 26 | // name, email, subject and body are required |
||
| 27 | [['name', 'email', 'subject', 'body'], 'required'], |
||
| 28 | // email has to be a valid email address |
||
| 29 | ['email', 'email'], |
||
| 30 | // verifyCode needs to be entered correctly |
||
| 31 | ['verifyCode', 'captcha'], |
||
| 32 | ]; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return array customized attribute labels |
||
| 37 | */ |
||
| 38 | public function attributeLabels() |
||
| 39 | { |
||
| 40 | return [ |
||
| 41 | 'verifyCode' => 'Verification Code', |
||
| 42 | ]; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Sends an email to the specified email address using the information collected by this model. |
||
| 47 | * @param string $email the target email address |
||
| 48 | * @return bool whether the model passes validation |
||
| 49 | */ |
||
| 50 | public function contact($email) |
||
| 51 | { |
||
| 52 | if ($this->validate()) { |
||
| 53 | Yii::$app->mailer->compose() |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | ->setTo($email) |
||
| 55 | ->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']]) |
||
|
0 ignored issues
–
show
|
|||
| 56 | ->setReplyTo([$this->email => $this->name]) |
||
| 57 | ->setSubject($this->subject) |
||
| 58 | ->setTextBody($this->body) |
||
| 59 | ->send(); |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | } |
||
| 66 |