|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace terabytesoft\mailer\user; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\Component; |
|
6
|
|
|
use yii\db\ActiveRecord; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Mailer |
|
10
|
|
|
* |
|
11
|
|
|
**/ |
|
12
|
|
|
class Mailer extends Component |
|
13
|
|
|
{ |
|
14
|
|
|
public $mailerComponent; |
|
15
|
|
|
public $sender; |
|
16
|
|
|
public $viewPath = '@terabytesoft/mailer/user/views'; |
|
17
|
|
|
|
|
18
|
|
|
protected $confirmationSubject; |
|
19
|
|
|
protected $newPasswordSubject; |
|
20
|
|
|
protected $reconfirmationSubject; |
|
21
|
|
|
protected $recoverySubject; |
|
22
|
|
|
protected $welcomeSubject; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* getWelcomeSubject |
|
27
|
|
|
**/ |
|
28
|
1 |
|
public function getWelcomeSubject(): string |
|
29
|
|
|
{ |
|
30
|
1 |
|
if ($this->welcomeSubject == null) { |
|
31
|
1 |
|
$this->setWelcomeSubject(\Yii::t( |
|
32
|
1 |
|
'mailer.user', |
|
33
|
1 |
|
'Welcome to {0}', |
|
34
|
1 |
|
[\Yii::$app->name] |
|
35
|
|
|
)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
return $this->welcomeSubject; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* setWelcomeSubject |
|
43
|
|
|
**/ |
|
44
|
1 |
|
public function setWelcomeSubject(string $welcomeSubject): string |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->welcomeSubject = $welcomeSubject; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* getNewPasswordSubject |
|
51
|
|
|
**/ |
|
52
|
1 |
|
public function getNewPasswordSubject(): string |
|
53
|
|
|
{ |
|
54
|
1 |
|
if ($this->newPasswordSubject == null) { |
|
55
|
1 |
|
$this->setNewPasswordSubject(\Yii::t( |
|
56
|
1 |
|
'mailer.user', |
|
57
|
1 |
|
'Your password on {0} has been changed', |
|
58
|
1 |
|
[\Yii::$app->name] |
|
59
|
|
|
)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->newPasswordSubject; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* setNewPasswordSubject |
|
67
|
|
|
**/ |
|
68
|
1 |
|
public function setNewPasswordSubject(string $newPasswordSubject): string |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->newPasswordSubject = $newPasswordSubject; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* getConfirmationSubject |
|
75
|
|
|
**/ |
|
76
|
1 |
|
public function getConfirmationSubject(): string |
|
77
|
|
|
{ |
|
78
|
1 |
|
if ($this->confirmationSubject == null) { |
|
79
|
1 |
|
$this->setConfirmationSubject(\Yii::t( |
|
80
|
1 |
|
'mailer.user', |
|
81
|
1 |
|
'Confirm account on {0}', |
|
82
|
1 |
|
[\Yii::$app->name] |
|
83
|
|
|
)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $this->confirmationSubject; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* setConfirmationSubject |
|
91
|
|
|
**/ |
|
92
|
1 |
|
public function setConfirmationSubject(string $confirmationSubject): string |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->confirmationSubject = $confirmationSubject; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* getReconfirmationSubject |
|
99
|
|
|
**/ |
|
100
|
2 |
|
public function getReconfirmationSubject(): string |
|
101
|
|
|
{ |
|
102
|
2 |
|
if ($this->reconfirmationSubject == null) { |
|
103
|
2 |
|
$this->setReconfirmationSubject(\Yii::t( |
|
104
|
2 |
|
'mailer.user', |
|
105
|
2 |
|
'Confirm email change on {0}', |
|
106
|
2 |
|
[\Yii::$app->name] |
|
107
|
|
|
)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
return $this->reconfirmationSubject; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* setReconfirmationSubject |
|
115
|
|
|
**/ |
|
116
|
2 |
|
public function setReconfirmationSubject($reconfirmationSubject): string |
|
117
|
|
|
{ |
|
118
|
2 |
|
return $this->reconfirmationSubject = $reconfirmationSubject; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* getRecoverySubject |
|
123
|
|
|
**/ |
|
124
|
1 |
|
public function getRecoverySubject(): string |
|
125
|
|
|
{ |
|
126
|
1 |
|
if ($this->recoverySubject == null) { |
|
127
|
1 |
|
$this->setRecoverySubject(\Yii::t( |
|
128
|
1 |
|
'mailer.user', |
|
129
|
1 |
|
'Complete password reset on {0}', |
|
130
|
1 |
|
[\Yii::$app->name] |
|
131
|
|
|
)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return $this->recoverySubject; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* setRecoverySubject |
|
139
|
|
|
**/ |
|
140
|
1 |
|
public function setRecoverySubject(string $recoverySubject): string |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->recoverySubject = $recoverySubject; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* sendWelcomeMessage |
|
147
|
|
|
* |
|
148
|
|
|
* sends an email to a user after registration |
|
149
|
|
|
**/ |
|
150
|
1 |
|
public function sendWelcomeMessage( |
|
151
|
|
|
ActiveRecord $user, |
|
152
|
|
|
ActiveRecord $token = null, |
|
153
|
|
|
object $module, |
|
154
|
|
|
$showPassword = false |
|
155
|
|
|
): bool { |
|
156
|
1 |
|
return $this->sendMessage( |
|
157
|
1 |
|
$user->email, |
|
|
|
|
|
|
158
|
1 |
|
$this->getWelcomeSubject(), |
|
159
|
1 |
|
'welcome', |
|
160
|
1 |
|
['user' => $user, 'token' => $token, 'showPassword' => $showPassword, 'module' => $module] |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* sendGeneratedPassword |
|
166
|
|
|
* |
|
167
|
|
|
* sends a new generated password to a user |
|
168
|
|
|
**/ |
|
169
|
1 |
|
public function sendGeneratedPassword(ActiveRecord $user, string $password): bool |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->sendMessage( |
|
172
|
1 |
|
$user->email, |
|
|
|
|
|
|
173
|
1 |
|
$this->getNewPasswordSubject(), |
|
174
|
1 |
|
'new_password', |
|
175
|
1 |
|
['user' => $user, 'password' => $password] |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* sendConfirmationMessage |
|
181
|
|
|
* |
|
182
|
|
|
* sends an email to a user with confirmation link |
|
183
|
|
|
**/ |
|
184
|
1 |
|
public function sendConfirmationMessage(ActiveRecord $user, ActiveRecord $token): bool |
|
185
|
|
|
{ |
|
186
|
1 |
|
return $this->sendMessage( |
|
187
|
1 |
|
$user->email, |
|
|
|
|
|
|
188
|
1 |
|
$this->getConfirmationSubject(), |
|
189
|
1 |
|
'confirmation', |
|
190
|
1 |
|
['user' => $user, 'token' => $token] |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* sendReconfirmationMessage. |
|
196
|
|
|
* |
|
197
|
|
|
* sends an email to a user with reconfirmation link |
|
198
|
|
|
**/ |
|
199
|
2 |
|
public function sendReconfirmationMessage(ActiveRecord $user, ActiveRecord $token): bool |
|
200
|
|
|
{ |
|
201
|
2 |
|
if ($token->type == $token::TYPE_CONFIRM_NEW_EMAIL) { |
|
|
|
|
|
|
202
|
1 |
|
$email = $user->unconfirmed_email; |
|
203
|
|
|
} else { |
|
204
|
1 |
|
$email = $user->email; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
2 |
|
return $this->sendMessage( |
|
208
|
2 |
|
$email, |
|
|
|
|
|
|
209
|
2 |
|
$this->getReconfirmationSubject(), |
|
210
|
2 |
|
'reconfirmation', |
|
211
|
2 |
|
['user' => $user, 'token' => $token] |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* sendRecoveryMessage |
|
217
|
|
|
* |
|
218
|
|
|
* sends an email to a user with recovery link |
|
219
|
|
|
**/ |
|
220
|
1 |
|
public function sendRecoveryMessage(ActiveRecord $user, ActiveRecord $token): bool |
|
221
|
|
|
{ |
|
222
|
1 |
|
\Yii::$app->session->set('sendRecoveryMessage', true); |
|
|
|
|
|
|
223
|
1 |
|
return $this->sendMessage( |
|
224
|
1 |
|
$user->email, |
|
|
|
|
|
|
225
|
1 |
|
$this->getRecoverySubject(), |
|
226
|
1 |
|
'recovery', |
|
227
|
1 |
|
['user' => $user, 'token' => $token] |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* sendMessage |
|
233
|
|
|
**/ |
|
234
|
6 |
|
protected function sendMessage(string $to, string $subject, string $view, array $params = []): bool |
|
235
|
|
|
{ |
|
236
|
6 |
|
$mailer = $this->mailerComponent === null ? \Yii::$app->mailer : \Yii::$app->get($this->mailerComponent); |
|
237
|
|
|
|
|
238
|
6 |
|
$mailer->viewPath = $this->viewPath; |
|
239
|
|
|
|
|
240
|
6 |
|
return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params) |
|
|
|
|
|
|
241
|
6 |
|
->setTo($to) |
|
242
|
6 |
|
->setFrom( |
|
243
|
6 |
|
[\Yii::$app->params['mailer.user.email.sender'] => \Yii::$app->params['mailer.user.email.sender.name']] |
|
244
|
|
|
) |
|
245
|
6 |
|
->setSubject($subject) |
|
246
|
6 |
|
->send(); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|