Completed
Push — master ( 6cbf2a...6f0b1a )
by Razon
02:25
created

ContactForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace App\Http\Form;
3
4
use Yii;
5
use yii\base\Model;
6
7
/**
8
 * ContactForm is the model behind the contact form.
9
 */
10
class ContactForm extends Model
11
{
12
    public $name;
13
    public $email;
14
    public $subject;
15
    public $body;
16
    public $verifyCode;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function rules()
22
    {
23
        return [
24
            // name, email, subject and body are required
25
            [['name', 'email', 'subject', 'body'], 'required'],
26
            // email has to be a valid email address
27
            ['email', 'email'],
28
            // verifyCode needs to be entered correctly
29
            ['verifyCode', 'captcha'],
30
        ];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function attributeLabels()
37
    {
38
        return [
39
            'verifyCode' => 'Verification Code',
40
        ];
41
    }
42
43
    /**
44
     * Sends an email to the specified email address using the information collected by this model.
45
     *
46
     * @param string $email the target email address
47
     * @return bool whether the email was sent
48
     */
49
    public function sendEmail($email)
50
    {
51
        return Yii::$app->getMailer()
52
            ->compose()
53
            ->setTo($email)
54
            ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
55
            ->setReplyTo([$this->email => $this->name])
56
            ->setSubject($this->subject)
57
            ->setTextBody($this->body)
58
            ->send();
59
    }
60
}
61