-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailer.php
More file actions
77 lines (63 loc) · 2.72 KB
/
Copy pathMailer.php
File metadata and controls
77 lines (63 loc) · 2.72 KB
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
<?php
namespace chipmob\user;
use chipmob\user\models\User;
use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;
/**
* Class Mailer.
*
* @property string $viewPath The directory that contains the view files for composing mail messages
*
* @property string $welcomeSubject
* @property string $newPasswordSubject
* @property string $confirmationSubject
* @property string $recoverySubject
*/
class Mailer extends Component
{
const DEFAULT_SENDER = 'no-reply@web.loc';
public string $viewPath = '@user/views/mail';
public string $welcomeSubject;
public string $newPasswordSubject;
public string $confirmationSubject;
public string $recoverySubject;
/** @inheritdoc */
public function __construct($config = [])
{
$this->welcomeSubject = Yii::t('user', 'Welcome to {0}', Yii::$app->name);
$this->newPasswordSubject = Yii::t('user', 'Your password on {0} has been changed', Yii::$app->name);
$this->confirmationSubject = Yii::t('user', 'Confirm account on {0}', Yii::$app->name);
$this->recoverySubject = Yii::t('user', 'Complete password reset on {0}', Yii::$app->name);
parent::__construct($config);
}
public function sendWelcomeMessage(User $user, array $params = []): bool
{
return $this->sendMessage($user->email, $this->welcomeSubject, 'welcome', compact('user', 'params'));
}
public function sendGeneratedPassword(User $user, array $params = []): bool
{
return $this->sendMessage($user->email, $this->newPasswordSubject, 'new_password', compact('user', 'params'));
}
public function sendConfirmationMessage(User $user, array $params = []): bool
{
return $this->sendMessage($user->email, $this->confirmationSubject, 'confirmation', compact('user', 'params'));
}
public function sendRecoveryMessage(User $user, array $params = []): bool
{
return $this->sendMessage($user->email, $this->recoverySubject, 'recovery', compact('user', 'params'));
}
protected function sendMessage(string $to, string $subject, string $view, array $params = []): bool
{
Yii::$app->mailer->viewPath = $this->viewPath;
Yii::$app->mailer->view->theme = Yii::$app->view->theme;
$sender = ArrayHelper::getValue(Yii::$app->params, 'noReplyEmail', self::DEFAULT_SENDER);
if (!YII_DEBUG && $sender === self::DEFAULT_SENDER) Yii::warning(Yii::t('user', 'Please set variable "noReplyEmail" in params, otherwise <{0}> will be used', self::DEFAULT_SENDER));
return Yii::$app->mailer
->compose(['html' => $view, 'text' => 'text/' . $view], $params)
->setTo($to)
->setFrom($sender)
->setSubject($subject)
->send();
}
}