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
|
1 |
|
public function sendReconfirmationMessage(ActiveRecord $user, ActiveRecord $token): bool |
200
|
|
|
{ |
201
|
1 |
|
return $this->sendMessage( |
202
|
1 |
|
$user->email, |
|
|
|
|
203
|
1 |
|
$this->getReconfirmationSubject(), |
204
|
1 |
|
'reconfirmation', |
205
|
1 |
|
['user' => $user, 'token' => $token] |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* sendReconfirmationMessageConfirmNewEmail |
211
|
|
|
* |
212
|
|
|
* sends an email to a user with reconfirmation link |
213
|
|
|
**/ |
214
|
1 |
|
public function sendReconfirmationMessageConfirmNewEmail(ActiveRecord $user, ActiveRecord $token): bool |
215
|
|
|
{ |
216
|
1 |
|
return $this->sendMessage( |
217
|
1 |
|
$user->unconfirmed_email, |
|
|
|
|
218
|
1 |
|
$this->getReconfirmationSubject(), |
219
|
1 |
|
'reconfirmation', |
220
|
1 |
|
['user' => $user, 'token' => $token] |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* sendRecoveryMessage |
226
|
|
|
* |
227
|
|
|
* sends an email to a user with recovery link |
228
|
|
|
**/ |
229
|
1 |
|
public function sendRecoveryMessage(ActiveRecord $user, ActiveRecord $token): bool |
230
|
|
|
{ |
231
|
1 |
|
\Yii::$app->session->set('sendRecoveryMessage', true); |
|
|
|
|
232
|
1 |
|
return $this->sendMessage( |
233
|
1 |
|
$user->email, |
|
|
|
|
234
|
1 |
|
$this->getRecoverySubject(), |
235
|
1 |
|
'recovery', |
236
|
1 |
|
['user' => $user, 'token' => $token] |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* sendMessage |
242
|
|
|
**/ |
243
|
6 |
|
protected function sendMessage(string $to, string $subject, string $view, array $params = []): bool |
244
|
|
|
{ |
245
|
6 |
|
$mailer = $this->mailerComponent === null ? \Yii::$app->mailer : \Yii::$app->get($this->mailerComponent); |
246
|
|
|
|
247
|
6 |
|
$mailer->viewPath = $this->viewPath; |
248
|
|
|
|
249
|
6 |
|
return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params) |
|
|
|
|
250
|
6 |
|
->setTo($to) |
251
|
6 |
|
->setFrom( |
252
|
6 |
|
[\Yii::$app->params['mailer.user.email.sender'] => \Yii::$app->params['mailer.user.email.sender.name']] |
253
|
|
|
) |
254
|
6 |
|
->setSubject($subject) |
255
|
6 |
|
->send(); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|