forked from dektrium/yii2-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
131 lines (116 loc) · 3.16 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
<?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\base\Component;
/**
* Mailer.
*
* @author Dmitry Erofeev <[email protected]>
*/
class Mailer extends Component
{
/** @var string */
public $viewPath = '@dektrium/user/views/mail';
/** @var string|array */
public $sender = '[email protected]';
/** @var string */
public $welcomeSubject;
/** @var string */
public $confirmationSubject;
/** @var string */
public $reconfirmationSubject;
/** @var string */
public $recoverySubject;
/**
* Sends an email to a user with credentials and confirmation link.
*
* @param User $user
* @param Token $token
* @return bool
*/
public function sendWelcomeMessage(User $user, Token $token = null)
{
return $this->sendMessage($user->email,
$this->welcomeSubject,
'welcome',
['user' => $user, 'token' => $token]
);
}
/**
* 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->confirmationSubject,
'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->reconfirmationSubject,
'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->recoverySubject,
'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 = \Yii::$app->mailer;
$mailer->viewPath = $this->viewPath;
$mailer->getView()->theme = \Yii::$app->view->theme;
return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params)
->setTo($to)
->setFrom($this->sender)
->setSubject($subject)
->send();
}
}