|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace terabytesoft\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\Component; |
|
6
|
|
|
use yii\mail\MailerInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Mailer |
|
10
|
|
|
* |
|
11
|
|
|
**/ |
|
12
|
|
|
class Mailer extends Component |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \yii\mail\MessageInterface $emailConfig |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $emailConfig; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var object $mailer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $mailer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array $view |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $views; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* __construct |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->setMailer(); |
|
35
|
3 |
|
$this->setViewPath(); |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* sendMessage |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function sendMessage(string $to, string $subject, array $options = [], array $params = []): bool |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->setView($options); |
|
44
|
|
|
|
|
45
|
3 |
|
$this->emailConfig = $this->mailer |
|
46
|
3 |
|
->compose($this->views, $params) |
|
47
|
3 |
|
->setTo($to) |
|
48
|
3 |
|
->setFrom( |
|
49
|
3 |
|
[\Yii::$app->params['helper.mailer.sender'] => \Yii::$app->params['helper.mailer.sender.name']] |
|
50
|
|
|
) |
|
51
|
3 |
|
->setSubject($subject); |
|
52
|
|
|
|
|
53
|
3 |
|
$this->setHtmlBody($options); |
|
54
|
3 |
|
$this->setReplyTo($options); |
|
55
|
3 |
|
$this->setTextBody($options); |
|
56
|
|
|
|
|
57
|
3 |
|
return $this->mailer->send($this->emailConfig); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* setHtmlBody |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function setHtmlBody(array $options): void |
|
64
|
|
|
{ |
|
65
|
3 |
|
if (isset($options['htmlBody'])) { |
|
66
|
1 |
|
$this->emailConfig = $this->emailConfig |
|
67
|
1 |
|
->setHtmlBody($options['htmlBody']); |
|
68
|
|
|
} |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* setMailer |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function setMailer(): MailerInterface |
|
75
|
|
|
{ |
|
76
|
3 |
|
return $this->mailer = \Yii::$app->mailer; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* setReplyTo |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function setReplyTo(array $options): void |
|
83
|
|
|
{ |
|
84
|
3 |
|
if (isset($options['replyTo'])) { |
|
85
|
1 |
|
$this->emailConfig = $this->emailConfig |
|
86
|
1 |
|
->setReplyTo($options['replyTo']); |
|
87
|
|
|
} |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* setTextBody |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function setTextBody(array $options): void |
|
94
|
|
|
{ |
|
95
|
3 |
|
if (isset($options['textBody'])) { |
|
96
|
2 |
|
$this->emailConfig = $this->emailConfig |
|
97
|
2 |
|
->setTextBody($options['textBody']); |
|
98
|
|
|
} |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* setView |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function setView(array $options): void |
|
105
|
|
|
{ |
|
106
|
3 |
|
$this->views = isset($options['views']) ? $options['views'] : []; |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* setViewPath |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function setViewPath(): void |
|
113
|
|
|
{ |
|
114
|
3 |
|
$this->mailer->viewPath = isset(\Yii::$app->params['helper.mailer.viewpath']) |
|
115
|
3 |
|
? \Yii::$app->params['helper.mailer.viewpath'] : []; |
|
116
|
3 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|