Completed
Push — master ( 822529...35d5fa )
by Igor
02:20
created

ContactForm::sendEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace app\models\forms;
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
     * @return array the validation rules.
21
     */
22
    public function rules()
23
    {
24
        return [
25
            // name, email, subject and body are required
26
            [['name', 'email', 'subject', 'body'], 'required'],
27
            // email has to be a valid email address
28
            ['email', 'email'],
29
            // verifyCode needs to be entered correctly
30
            ['verifyCode', 'captcha'],
31
        ];
32
    }
33
34
    /**
35
     * @return array customized attribute labels
36
     */
37
    public function attributeLabels()
38
    {
39
        return [
40
            'verifyCode' => 'Verification Code',
41
        ];
42
    }
43
44
    /**
45
     * Sends an email to the specified email address using the information collected by this model.
46
     *
47
     * @param  string $email the target email address
48
     * @return boolean whether the model passes validation
49
     */
50
    public function sendEmail($email)
51
    {
52
        return Yii::$app->mailer->compose()
53
            ->setTo($email)
54
            ->setFrom([$this->email => $this->name])
55
            ->setSubject($this->subject)
56
            ->setTextBody($this->body)
57
            ->send();
58
    }
59
}