forked from dektrium/yii2-user
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mailer.php
283 lines (246 loc) · 6.87 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace dektrium\user;
use dektrium\user\models\Token;
use dektrium\user\models\User;
use Yii;
use yii\base\Component;
/**
* Mailer.
*
* @author Dmitry Erofeev <dmeroff@gmail.com>
*/
class Mailer extends Component
{
/** @var string */
public $viewPath = '@dektrium/user/views/mail';
/** @var string|array Default: `Yii::$app->params['adminEmail']` OR `no-reply@example.com` */
public $sender;
/** @var \yii\mail\BaseMailer Default: `Yii::$app->mailer` */
public $mailerComponent;
/** @var string */
protected $welcomeSubject;
/** @var string */
protected $newPasswordSubject;
/** @var string */
protected $confirmationSubject;
/** @var string */
protected $reconfirmationSubject;
/** @var string */
protected $recoverySubject;
/** @var \dektrium\user\Module */
protected $module;
/**
* @return string
*/
public function getWelcomeSubject()
{
if ($this->welcomeSubject == null) {
$this->setWelcomeSubject(Yii::t('user', 'Welcome to {0}', Yii::$app->name));
}
return $this->welcomeSubject;
}
/**
* @param string $welcomeSubject
*/
public function setWelcomeSubject($welcomeSubject)
{
$this->welcomeSubject = $welcomeSubject;
}
/**
* @return string
*/
public function getNewPasswordSubject()
{
if ($this->newPasswordSubject == null) {
$this->setNewPasswordSubject(Yii::t('user', 'Your password on {0} has been changed', Yii::$app->name));
}
return $this->newPasswordSubject;
}
/**
* @param string $newPasswordSubject
*/
public function setNewPasswordSubject($newPasswordSubject)
{
$this->newPasswordSubject = $newPasswordSubject;
}
/**
* @return string
*/
public function getConfirmationSubject()
{
if ($this->confirmationSubject == null) {
$this->setConfirmationSubject(Yii::t('user', 'Confirm account on {0}', Yii::$app->name));
}
return $this->confirmationSubject;
}
/**
* @param string $confirmationSubject
*/
public function setConfirmationSubject($confirmationSubject)
{
$this->confirmationSubject = $confirmationSubject;
}
/**
* @return string
*/
public function getReconfirmationSubject()
{
if ($this->reconfirmationSubject == null) {
$this->setReconfirmationSubject(Yii::t('user', 'Confirm email change on {0}', Yii::$app->name));
}
return $this->reconfirmationSubject;
}
/**
* @param string $reconfirmationSubject
*/
public function setReconfirmationSubject($reconfirmationSubject)
{
$this->reconfirmationSubject = $reconfirmationSubject;
}
/**
* @return string
*/
public function getRecoverySubject()
{
if ($this->recoverySubject == null) {
$this->setRecoverySubject(Yii::t('user', 'Complete password reset on {0}', Yii::$app->name));
}
return $this->recoverySubject;
}
/**
* @param string $recoverySubject
*/
public function setRecoverySubject($recoverySubject)
{
$this->recoverySubject = $recoverySubject;
}
/** @inheritdoc */
public function init()
{
$this->module = Yii::$app->getModule('user');
parent::init();
}
/**
* Sends an email to a user after registration.
*
* @param User $user
* @param Token $token
* @param bool $showPassword
*
* @return bool
*/
public function sendWelcomeMessage(User $user, Token $token = null, $showPassword = false)
{
return $this->sendMessage(
$user->email,
$this->getWelcomeSubject(),
'welcome',
['user' => $user, 'token' => $token, 'module' => $this->module, 'showPassword' => $showPassword]
);
}
/**
* Sends a new generated password to a user.
*
* @param User $user
* @param Password $password
*
* @return bool
*/
public function sendGeneratedPassword(User $user, $password)
{
return $this->sendMessage(
$user->email,
$this->getNewPasswordSubject(),
'new_password',
['user' => $user, 'password' => $password, 'module' => $this->module]
);
}
/**
* Sends an email to a user with confirmation link.
*
* @param User $user
* @param Token $token
*
* @return bool
*/
public function sendConfirmationMessage(User $user, Token $token)
{
return $this->sendMessage(
$user->email,
$this->getConfirmationSubject(),
'confirmation',
['user' => $user, 'token' => $token]
);
}
/**
* Sends an email to a user with reconfirmation link.
*
* @param User $user
* @param Token $token
*
* @return bool
*/
public function sendReconfirmationMessage(User $user, Token $token)
{
if ($token->type == Token::TYPE_CONFIRM_NEW_EMAIL) {
$email = $user->unconfirmed_email;
} else {
$email = $user->email;
}
return $this->sendMessage(
$email,
$this->getReconfirmationSubject(),
'reconfirmation',
['user' => $user, 'token' => $token]
);
}
/**
* Sends an email to a user with recovery link.
*
* @param User $user
* @param Token $token
*
* @return bool
*/
public function sendRecoveryMessage(User $user, Token $token)
{
return $this->sendMessage(
$user->email,
$this->getRecoverySubject(),
'recovery',
['user' => $user, 'token' => $token]
);
}
/**
* @param string $to
* @param string $subject
* @param string $view
* @param array $params
*
* @return bool
*/
protected function sendMessage($to, $subject, $view, $params = [])
{
$mailer = $this->mailerComponent === null ? Yii::$app->mailer : Yii::$app->get($this->mailerComponent);
$mailer->viewPath = $this->viewPath;
$mailer->getView()->theme = Yii::$app->view->theme;
if ($this->sender === null) {
$this->sender = isset(Yii::$app->params['adminEmail']) ?
Yii::$app->params['adminEmail']
: 'no-reply@example.com';
}
return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params)
->setTo($to)
->setFrom($this->sender)
->setSubject($subject)
->send();
}
}