1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\basic\forms; |
4
|
|
|
|
5
|
|
|
use yii\base\Application; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\mail\MailerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ContactForm is the model behind the contact form Web Application Basic. |
11
|
|
|
**/ |
12
|
|
|
class ContactForm extends Model |
13
|
|
|
{ |
14
|
|
|
public $name; |
15
|
|
|
public $email; |
16
|
|
|
public $subject; |
17
|
|
|
public $body; |
18
|
|
|
public $verifyCode; |
19
|
|
|
|
20
|
|
|
protected $app; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* __construct |
24
|
|
|
* |
25
|
|
|
* @param Application $app |
26
|
|
|
**/ |
27
|
|
|
public function __construct(Application $app) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* rules |
34
|
|
|
* |
35
|
|
|
* @return array the validation rules. |
36
|
|
|
**/ |
37
|
|
|
public function rules() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
|
|
// name, email, subject and body are required |
41
|
|
|
[['name', 'email', 'subject', 'body'], 'required'], |
42
|
|
|
// email has to be a valid email address |
43
|
|
|
['email', 'email'], |
44
|
|
|
// verifyCode needs to be entered correctly |
45
|
|
|
//['verifyCode', \yii\captcha\CaptchaValidator::class], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* attributeLabels |
51
|
|
|
* Translate Atribute Labels. |
52
|
|
|
* |
53
|
|
|
* @return array customized attribute labels. |
54
|
|
|
**/ |
55
|
|
|
public function attributeLabels() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'body' => $this->app->t('basic', 'Body'), |
59
|
|
|
'email' => $this->app->t('basic', 'Email'), |
60
|
|
|
'name' => $this->app->t('basic', 'Name'), |
61
|
|
|
'password' => $this->app->t('basic', 'Password'), |
62
|
|
|
'subject' => $this->app->t('basic', 'Subject'), |
63
|
|
|
'verifyCode' => $this->app->t('basic', 'VerifyCode'), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* contact |
69
|
|
|
* Sends an email to the specified email address using the information collected by this model. |
70
|
|
|
* |
71
|
|
|
* @param string $email the target email address. |
72
|
|
|
* @return bool whether the model passes validation. |
73
|
|
|
**/ |
74
|
|
|
public function contact(string $email, MailerInterface $mailer) |
75
|
|
|
{ |
76
|
|
|
if ($this->validate()) { |
77
|
|
|
$mailer->compose() |
78
|
|
|
->setTo($email) |
79
|
|
|
->setFrom([$this->email => $this->name]) |
80
|
|
|
->setSubject($this->subject) |
81
|
|
|
->setTextBody($this->body) |
82
|
|
|
->send(); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|