1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace terabytesoft\mailer\user; |
4
|
|
|
|
5
|
|
|
use terabytesoft\app\user\models\TokenModel; |
|
|
|
|
6
|
|
|
use terabytesoft\app\user\models\UserModel; |
|
|
|
|
7
|
|
|
use terabytesoft\app\user\traits\ModuleTrait; |
|
|
|
|
8
|
|
|
use yii\base\Component; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Mailer. |
12
|
|
|
* |
13
|
|
|
**/ |
14
|
|
|
class Mailer extends Component |
15
|
|
|
{ |
16
|
|
|
use ModuleTrait; |
17
|
|
|
|
18
|
|
|
public $mailerComponent; |
19
|
|
|
public $sender; |
20
|
|
|
public $viewPath = '@terabytesoft/mailer/user/views'; |
21
|
|
|
|
22
|
|
|
protected $confirmationSubject; |
23
|
|
|
protected $newPasswordSubject; |
24
|
|
|
protected $reconfirmationSubject; |
25
|
|
|
protected $recoverySubject; |
26
|
|
|
protected $welcomeSubject; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* getWelcomeSubject. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
**/ |
34
|
|
|
public function getWelcomeSubject(): string |
35
|
|
|
{ |
36
|
|
|
if ($this->welcomeSubject == null) { |
37
|
|
|
$this->setWelcomeSubject(\Yii::t( |
38
|
|
|
'app.user', |
39
|
|
|
'Welcome to {0}', |
40
|
|
|
[$this->app->name] |
|
|
|
|
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->welcomeSubject; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* setWelcomeSubject. |
49
|
|
|
* |
50
|
|
|
* @param string $welcomeSubject |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
**/ |
54
|
|
|
public function setWelcomeSubject(string $welcomeSubject) |
55
|
|
|
{ |
56
|
|
|
$this->welcomeSubject = $welcomeSubject; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* getNewPasswordSubject. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
**/ |
64
|
|
|
public function getNewPasswordSubject(): string |
65
|
|
|
{ |
66
|
|
|
if ($this->newPasswordSubject == null) { |
67
|
|
|
$this->setNewPasswordSubject(\Yii::t( |
68
|
|
|
'app.user', |
69
|
|
|
'Your password on {0} has been changed', |
70
|
|
|
[$this->app->name] |
|
|
|
|
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->newPasswordSubject; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* setNewPasswordSubject. |
79
|
|
|
* |
80
|
|
|
* @param string $newPasswordSubject |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
**/ |
84
|
|
|
public function setNewPasswordSubject(string $newPasswordSubject) |
85
|
|
|
{ |
86
|
|
|
$this->newPasswordSubject = $newPasswordSubject; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* getConfirmationSubject. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
**/ |
94
|
|
|
public function getConfirmationSubject(): string |
95
|
|
|
{ |
96
|
|
|
if ($this->confirmationSubject == null) { |
97
|
|
|
$this->setConfirmationSubject(\Yii::t( |
98
|
|
|
'app.user', |
99
|
|
|
'Confirm account on {0}', |
100
|
|
|
[$this->app->name] |
|
|
|
|
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->confirmationSubject; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* setConfirmationSubject. |
109
|
|
|
* |
110
|
|
|
* @param string $newPasswordSubject |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
**/ |
114
|
|
|
public function setConfirmationSubject(string $confirmationSubject) |
115
|
|
|
{ |
116
|
|
|
$this->confirmationSubject = $confirmationSubject; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* getReconfirmationSubject. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
**/ |
124
|
|
|
public function getReconfirmationSubject(): string |
125
|
|
|
{ |
126
|
|
|
if ($this->reconfirmationSubject == null) { |
127
|
|
|
$this->setReconfirmationSubject(\Yii::t( |
128
|
|
|
'app.user', |
129
|
|
|
'Confirm email change on {0}', |
130
|
|
|
[$this->app->name] |
|
|
|
|
131
|
|
|
)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->reconfirmationSubject; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* setReconfirmationSubject. |
139
|
|
|
* |
140
|
|
|
* @param string $reconfirmationSubject |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
**/ |
144
|
|
|
public function setReconfirmationSubject($reconfirmationSubject) |
145
|
|
|
{ |
146
|
|
|
$this->reconfirmationSubject = $reconfirmationSubject; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* getRecoverySubject. |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
**/ |
154
|
|
|
public function getRecoverySubject() |
155
|
|
|
{ |
156
|
|
|
if ($this->recoverySubject == null) { |
157
|
|
|
$this->setRecoverySubject(\Yii::t( |
158
|
|
|
'app.user', |
159
|
|
|
'Complete password reset on {0}', |
160
|
|
|
[$this->app->name] |
|
|
|
|
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->recoverySubject; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* setRecoverySubject. |
169
|
|
|
* |
170
|
|
|
* @param string $recoverySubject |
171
|
|
|
* |
172
|
|
|
* @return string|null |
173
|
|
|
**/ |
174
|
|
|
public function setRecoverySubject(string $recoverySubject) |
175
|
|
|
{ |
176
|
|
|
$this->recoverySubject = $recoverySubject; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* sendWelcomeMessage. |
181
|
|
|
* |
182
|
|
|
* sends an email to a user after registration. |
183
|
|
|
* |
184
|
|
|
* @param UserModel $user |
185
|
|
|
* @param TokenModel $token |
186
|
|
|
* @param bool $showPassword |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
**/ |
190
|
|
|
public function sendWelcomeMessage(UserModel $user, TokenModel $token = null, $showPassword = false): bool |
191
|
|
|
{ |
192
|
|
|
return $this->sendMessage( |
193
|
|
|
$user->email, |
194
|
|
|
$this->getWelcomeSubject(), |
195
|
|
|
'welcome', |
196
|
|
|
['user' => $user, 'token' => $token, 'module' => $this->module, 'showPassword' => $showPassword] |
|
|
|
|
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* sendGeneratedPassword. |
202
|
|
|
* |
203
|
|
|
* sends a new generated password to a user |
204
|
|
|
* |
205
|
|
|
* @param UserModel $user |
206
|
|
|
* @param \app\user\helpers\PasswordHelper $password |
|
|
|
|
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
**/ |
210
|
|
|
public function sendGeneratedPassword(UserModel $user, $password): bool |
211
|
|
|
{ |
212
|
|
|
return $this->sendMessage( |
213
|
|
|
$user->email, |
214
|
|
|
$this->getNewPasswordSubject(), |
215
|
|
|
'New_Password', |
216
|
|
|
['user' => $user, 'password' => $password, 'module' => $this->module] |
|
|
|
|
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* sendConfirmationMessage. |
222
|
|
|
* |
223
|
|
|
* sends an email to a user with confirmation link |
224
|
|
|
* |
225
|
|
|
* @param UserModel $user |
226
|
|
|
* @param TokenModel $token |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
**/ |
230
|
|
|
public function sendConfirmationMessage(UserModel $user, TokenModel $token): bool |
231
|
|
|
{ |
232
|
|
|
return $this->sendMessage( |
233
|
|
|
$user->email, |
234
|
|
|
$this->getConfirmationSubject(), |
235
|
|
|
'Confirmation', |
236
|
|
|
['user' => $user, 'token' => $token, 'module' => $this->module] |
|
|
|
|
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* sendReconfirmationMessage. |
242
|
|
|
* |
243
|
|
|
* sends an email to a user with reconfirmation link. |
244
|
|
|
* |
245
|
|
|
* @param UserModel $user |
246
|
|
|
* @param TokenModel $token |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
**/ |
250
|
|
|
public function sendReconfirmationMessage(UserModel $user, TokenModel $token): bool |
251
|
|
|
{ |
252
|
|
|
if ($token->type == TokenModel::TYPE_CONFIRM_NEW_EMAIL) { |
253
|
|
|
$email = $user->unconfirmed_email; |
254
|
|
|
} else { |
255
|
|
|
$email = $user->email; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->sendMessage( |
259
|
|
|
$email, |
260
|
|
|
$this->getReconfirmationSubject(), |
261
|
|
|
'Reconfirmation', |
262
|
|
|
['user' => $user, 'token' => $token, 'module' => $this->module] |
|
|
|
|
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* sendRecoveryMessage. |
268
|
|
|
* |
269
|
|
|
* sends an email to a user with recovery link |
270
|
|
|
* |
271
|
|
|
* @param UserModel $user |
272
|
|
|
* @param TokenModel $token |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
**/ |
276
|
|
|
public function sendRecoveryMessage(UserModel $user, TokenModel $token) |
277
|
|
|
{ |
278
|
|
|
$this->app->session->set('sendRecoveryMessage', true); |
|
|
|
|
279
|
|
|
return $this->sendMessage( |
280
|
|
|
$user->email, |
281
|
|
|
$this->getRecoverySubject(), |
282
|
|
|
'Recovery', |
283
|
|
|
['user' => $user, 'token' => $token, 'module' => $this->module] |
|
|
|
|
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* sendMessage. |
289
|
|
|
* |
290
|
|
|
* @param string $to |
291
|
|
|
* @param string $subject |
292
|
|
|
* @param string $view |
293
|
|
|
* @param array $params |
294
|
|
|
* |
295
|
|
|
* @return bool |
296
|
|
|
**/ |
297
|
|
|
protected function sendMessage($to, $subject, $view, $params = []): bool |
298
|
|
|
{ |
299
|
|
|
$mailer = $this->mailerComponent === null ? $this->app->mailer : $this->app->get($this->mailerComponent); |
|
|
|
|
300
|
|
|
|
301
|
|
|
$mailer->viewPath = $this->viewPath; |
302
|
|
|
|
303
|
|
|
if ($this->sender === null) { |
304
|
|
|
$this->sender = isset($this->app->params['adminEmail']) ? |
305
|
|
|
$this->app->params['adminEmail'] |
306
|
|
|
: '[email protected]'; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params) |
310
|
|
|
->setTo($to) |
311
|
|
|
->setFrom($this->sender) |
312
|
|
|
->setSubject($subject) |
313
|
|
|
->send(); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths