ContactForm.php 1.3 KB
Newer Older
1 2
<?php

3
namespace frontend\models;
4

5
use Yii;
6 7 8 9 10 11 12
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model
{
13 14 15 16 17
    public $name;
    public $email;
    public $subject;
    public $body;
    public $verifyCode;
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32
    /**
     * @inheritdoc
     */
    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'],
        ];
    }
33

34 35 36 37 38 39 40 41 42
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'verifyCode' => 'Verification Code',
        ];
    }
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    /**
     * 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 email was sent
     */
    public function sendEmail($email)
    {
        return Yii::$app->mail->compose()
            ->setTo($email)
            ->setFrom([$this->email => $this->name])
            ->setSubject($this->subject)
            ->setTextBody($this->body)
            ->send();
    }
59
}