|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Nymfonya\Component\Container; |
|
7
|
|
|
use Nymfonya\Component\Config; |
|
8
|
|
|
use Swift_SmtpTransport; |
|
9
|
|
|
use Swift_Mailer; |
|
10
|
|
|
use Swift_Message; |
|
11
|
|
|
|
|
12
|
|
|
class Smtp |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const _MAILER = 'mailer'; |
|
16
|
|
|
const _SMTP = 'smtp'; |
|
17
|
|
|
const _HOST = 'host'; |
|
18
|
|
|
const _PORT = 'port'; |
|
19
|
|
|
const _USERNAME = 'username'; |
|
20
|
|
|
const _PASSWORD = 'password'; |
|
21
|
|
|
const _ENCRYPTION = 'encryption'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* transport mailer |
|
25
|
|
|
* |
|
26
|
|
|
* @var Swift_SmtpTransport |
|
27
|
|
|
*/ |
|
28
|
|
|
private $transport; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* mailer instance |
|
32
|
|
|
* |
|
33
|
|
|
* @var Swift_Mailer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $mailer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* mail message |
|
39
|
|
|
* |
|
40
|
|
|
* @var Swift_Message |
|
41
|
|
|
*/ |
|
42
|
|
|
private $message; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* mail message from |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $from; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* mail message to |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $to; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* mailer send error |
|
60
|
|
|
* |
|
61
|
|
|
* @var Boolean |
|
62
|
|
|
*/ |
|
63
|
|
|
private $error; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* instanciate |
|
68
|
|
|
* |
|
69
|
|
|
* @param Container $container |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(Container $container) |
|
72
|
|
|
{ |
|
73
|
|
|
$config = $container->getService(Config::class); |
|
74
|
|
|
$mailerConfig = $config->getSettings(self::_MAILER); |
|
75
|
|
|
$this->init($mailerConfig[self::_SMTP]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* set message initiators |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $from |
|
82
|
|
|
* @return Smtp |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setFrom(array $from): Smtp |
|
85
|
|
|
{ |
|
86
|
|
|
$this->from = $from; |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* set message recipients |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $to |
|
94
|
|
|
* @return Smtp |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setTo(array $to): Smtp |
|
97
|
|
|
{ |
|
98
|
|
|
$this->to = $to; |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* prepare message |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $title |
|
106
|
|
|
* @param string $body |
|
107
|
|
|
* @return Smtp |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setMessage(string $title, string $body): Smtp |
|
110
|
|
|
{ |
|
111
|
|
|
$this->message = new Swift_Message($title); |
|
112
|
|
|
$this->message |
|
113
|
|
|
->setFrom($this->from) |
|
114
|
|
|
->setTo($this->to) |
|
115
|
|
|
->setBody($body); |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* true means message was not sent |
|
121
|
|
|
* |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isError(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->error === true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* send message |
|
131
|
|
|
* |
|
132
|
|
|
* @return Smtp |
|
133
|
|
|
*/ |
|
134
|
|
|
public function sendMessage(): Smtp |
|
135
|
|
|
{ |
|
136
|
|
|
$this->mailer->send($this->message); |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
protected function init(array $transportConfig): Smtp |
|
141
|
|
|
{ |
|
142
|
|
|
$this->setTransport($transportConfig)->setMailer(); |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* prepare mailer transport |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $transportConfig |
|
150
|
|
|
* @return Smtp |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function setTransport(array $transportConfig): Smtp |
|
153
|
|
|
{ |
|
154
|
|
|
$this->checkConfig($transportConfig); |
|
155
|
|
|
$host = $transportConfig[self::_HOST]; |
|
156
|
|
|
$port = $transportConfig[self::_PORT]; |
|
157
|
|
|
$username = $transportConfig[self::_USERNAME]; |
|
158
|
|
|
$password = $transportConfig[self::_PASSWORD]; |
|
159
|
|
|
$encryption = $transportConfig[self::_ENCRYPTION]; |
|
160
|
|
|
$this->transport = new Swift_SmtpTransport($host, $port, $encryption); |
|
161
|
|
|
$this->transport->setUsername($username)->setPassword($password); |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* check if transport config is ready |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $transportConfig |
|
169
|
|
|
* @return Smtp |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function checkConfig(array $transportConfig): Smtp |
|
172
|
|
|
{ |
|
173
|
|
|
$isValid = isset($transportConfig[self::_HOST]) |
|
174
|
|
|
&& isset($transportConfig[self::_PORT]) |
|
175
|
|
|
&& isset($transportConfig[self::_USERNAME]) |
|
176
|
|
|
&& isset($transportConfig[self::_PASSWORD]) |
|
177
|
|
|
&& isset($transportConfig[self::_ENCRYPTION]); |
|
178
|
|
|
if (false === $isValid) { |
|
179
|
|
|
throw new Exception('Invalid transport config'); |
|
180
|
|
|
} |
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* prepare mailer |
|
186
|
|
|
* |
|
187
|
|
|
* @return Smtp |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function setMailer(): Smtp |
|
190
|
|
|
{ |
|
191
|
|
|
$mailSendResult = $this->mailer = new Swift_Mailer($this->transport); |
|
192
|
|
|
$this->error = ($mailSendResult === 0); |
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|