forked from dektrium/yii2-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.php
117 lines (106 loc) · 2.88 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
<?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]';
/**
* 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,
\Yii::t('user', 'Welcome to {0}', \Yii::$app->name),
'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,
\Yii::t('user', 'Confirm your account on {0}', \Yii::$app->name),
'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)
{
return $this->sendMessage($user->unconfirmed_email,
\Yii::t('user', 'Confirm your email change on {0}', \Yii::$app->name),
'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,
\Yii::t('user', 'Complete your password reset on {0}', \Yii::$app->name),
'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;
return $mailer->compose($view, $params)
->setTo($to)
->setFrom($this->sender)
->setSubject($subject)
->send();
}
}