1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core\Controller\Plugin; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Zend\EventManager\EventManagerInterface; |
7
|
|
|
use Zend\Log\LoggerInterface; |
8
|
|
|
use Zend\Mail\Transport\TransportInterface; |
9
|
|
|
use Zend\ModuleManager\ModuleManagerInterface; |
10
|
|
|
use Zend\Mvc\Controller\Plugin\PluginInterface; |
11
|
|
|
use Zend\Stdlib\DispatchableInterface as Dispatchable; |
12
|
|
|
use Zend\Mail\Message; |
13
|
|
|
use Zend\Mail\Transport\Sendmail; |
14
|
|
|
use Zend\Mail\AddressList; |
15
|
|
|
use Zend\EventManager\Event; |
16
|
|
|
use Zend\Stdlib\Parameters; |
17
|
|
|
use Zend\Stdlib\ArrayUtils; |
18
|
|
|
use Zend\View\Resolver\ResolverInterface; |
19
|
|
|
|
20
|
|
|
class Mail extends Message implements PluginInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Dispatchable |
24
|
|
|
*/ |
25
|
|
|
protected $controller; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $param = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $config = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $mailLogger; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ResolverInterface |
44
|
|
|
*/ |
45
|
|
|
protected $viewResolver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var EventManagerInterface |
49
|
|
|
*/ |
50
|
|
|
protected $eventManager; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ModuleManagerInterface |
54
|
|
|
*/ |
55
|
|
|
protected $moduleManager; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var TransportInterface |
59
|
|
|
*/ |
60
|
|
|
protected $transport; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Mail constructor. |
64
|
|
|
* @param LoggerInterface $mailLogger |
65
|
|
|
* @param ResolverInterface $viewResolver |
66
|
|
|
* @param EventManagerInterface $eventManager |
67
|
|
|
* @param ModuleManagerInterface $moduleManager |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
LoggerInterface $mailLogger, |
71
|
|
|
ResolverInterface $viewResolver, |
72
|
|
|
EventManagerInterface $eventManager, |
73
|
|
|
ModuleManagerInterface $moduleManager |
74
|
|
|
) |
75
|
|
|
{ |
76
|
|
|
$this->mailLogger = $mailLogger; |
77
|
|
|
$this->viewResolver = $viewResolver; |
78
|
|
|
$this->eventManager = $eventManager; |
79
|
|
|
$this->moduleManager = $moduleManager; |
80
|
|
|
$this->transport = new Sendmail(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the current controller instance |
85
|
|
|
* |
86
|
|
|
* @param Dispatchable $controller |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function setController(Dispatchable $controller) |
90
|
|
|
{ |
91
|
|
|
$this->controller = $controller; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the current controller instance |
96
|
|
|
* |
97
|
|
|
* @return null|Dispatchable |
98
|
|
|
*/ |
99
|
|
|
public function getController() |
100
|
|
|
{ |
101
|
|
|
return $this->controller; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set mail transport to be use |
106
|
|
|
* |
107
|
|
|
* @param TransportInterface $transport |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setTransport(TransportInterface $transport) |
111
|
|
|
{ |
112
|
|
|
$this->transport = $transport; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return TransportInterface |
119
|
|
|
*/ |
120
|
|
|
public function getTransport() |
121
|
|
|
{ |
122
|
|
|
return $this->transport; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __invoke(array $param = array()) |
126
|
|
|
{ |
127
|
|
|
$this->param = $param; |
128
|
|
|
$this->config = array(); |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function stringFromMailHeader($header) |
133
|
|
|
{ |
134
|
|
|
$erg = ''; |
135
|
|
|
if ($header instanceof AddressList) { |
136
|
|
|
$A = ArrayUtils::iteratorToArray($header); |
|
|
|
|
137
|
|
|
$AA = array(); |
|
|
|
|
138
|
|
|
while (!empty($A)) { |
|
|
|
|
139
|
|
|
$AA[] = array_shift($A)->toString(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
$erg = implode(',', $AA); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
return $erg; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function __toString() |
147
|
|
|
{ |
148
|
|
|
$template = $this->getTemplate(); |
149
|
|
|
$to = $this->stringFromMailHeader($this->getTo()); |
150
|
|
|
$cc = $this->stringFromMailHeader($this->getCc()); |
151
|
|
|
$bcc = $this->stringFromMailHeader($this->getBcc()); |
152
|
|
|
$from = $this->stringFromMailHeader($this->getFrom()); |
153
|
|
|
$replyTo = $this->stringFromMailHeader($this->getReplyTo()); |
154
|
|
|
|
155
|
|
|
return str_pad($template, 30) |
156
|
|
|
. 'to: ' . str_pad($to, 50) |
157
|
|
|
. 'cc: ' . str_pad($cc, 50) |
158
|
|
|
. 'bcc: ' . str_pad($bcc, 50) |
159
|
|
|
. 'from: ' . str_pad($from, 50) |
160
|
|
|
. 'replyTo: ' . str_pad($replyTo, 50) |
161
|
|
|
//. str_pad(implode(',', ArrayUtils::iteratorToArray($this->getSender())),50) |
|
|
|
|
162
|
|
|
. 'subject: ' . str_pad($this->getSubject(), 50); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function template($template) |
166
|
|
|
{ |
167
|
|
|
$controller = get_class($this->controller); |
168
|
|
|
|
169
|
|
|
$event = new Event(); |
170
|
|
|
$eventManager = $this->eventManager; |
171
|
|
|
// @TODO: check if change this value into ['Mail'] not causing any errors! |
172
|
|
|
$eventManager->setIdentifiers(['Mail']); |
173
|
|
|
$p = new Parameters(array('mail' => $this, 'template' => $template)); |
174
|
|
|
$event->setParams($p); |
175
|
|
|
$eventManager->trigger('template.pre', $event); |
176
|
|
|
|
177
|
|
|
// get all loaded modules |
178
|
|
|
$moduleManager = $this->moduleManager; |
179
|
|
|
$loadedModules = $moduleManager->getModules(); |
180
|
|
|
//get_called_class |
181
|
|
|
$controllerIdentifier = strtolower(substr($controller, 0, strpos($controller, '\\'))); |
182
|
|
|
$viewResolver = $this->viewResolver; |
183
|
|
|
|
184
|
|
|
$templateHalf = 'mail/' . $template; |
185
|
|
|
$resource = $viewResolver->resolve($templateHalf); |
186
|
|
|
|
187
|
|
|
if (empty($resource)) { |
188
|
|
|
$templateFull = $controllerIdentifier . '/mail/' . $template; |
189
|
|
|
$resource = $viewResolver->resolve($templateFull); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$__vars = $this->param; |
|
|
|
|
193
|
|
|
if (array_key_exists('this', $__vars)) { |
|
|
|
|
194
|
|
|
unset($__vars['this']); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
extract($__vars); |
|
|
|
|
197
|
|
|
unset($__vars); // remove $__vars from local scope |
|
|
|
|
198
|
|
|
|
199
|
|
|
if ($resource) { |
200
|
|
|
try { |
201
|
|
|
ob_start(); |
202
|
|
|
include $resource; |
203
|
|
|
$content = ob_get_clean(); |
204
|
|
|
$this->setBody($content); |
205
|
|
|
} catch (\Exception $ex) { |
206
|
|
|
ob_end_clean(); |
207
|
|
|
throw $ex; |
208
|
|
|
} |
209
|
|
|
$__vars = get_defined_vars(); |
|
|
|
|
210
|
|
|
foreach ($this->param as $key => $value) { |
211
|
|
|
if (isset($__vars[$key])) { |
|
|
|
|
212
|
|
|
unset($__vars[$key]); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
unset($__vars['content'],$__vars['controllerIdentifier'],$__vars['controller'],$__vars['resource'],$__vars['template'],$__vars['viewResolver']); |
|
|
|
|
216
|
|
|
$this->config = $__vars; |
|
|
|
|
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function getTemplate() |
221
|
|
|
{ |
222
|
|
|
$template = null; |
|
|
|
|
223
|
|
|
if (isset($this->config['templateFull'])) { |
224
|
|
|
$template = $this->config['templateFull']; |
225
|
|
|
} elseif (isset($this->config['templateHalf'])) { |
226
|
|
|
$template = $this->config['templateHalf']; |
227
|
|
|
} else { |
228
|
|
|
throw new \InvalidArgumentException('No template provided for Mail.'); |
229
|
|
|
} |
230
|
|
|
return $template; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function informationComplete() |
234
|
|
|
{ |
235
|
|
|
$log = $this->mailLogger; |
236
|
|
|
$template = $this->getTemplate(); |
237
|
|
View Code Duplication |
if (isset($this->config['from'])) { |
|
|
|
|
238
|
|
|
$from = $this->config['from']; |
239
|
|
|
} else { |
240
|
|
|
$log->err('A from email address must be provided (Variable $from) in Template: ' . $template); |
241
|
|
|
throw new \InvalidArgumentException('A from email address must be provided (Variable $from) in Template: ' . $template); |
242
|
|
|
} |
243
|
|
View Code Duplication |
if (isset($this->config['fromName'])) { |
|
|
|
|
244
|
|
|
$fromName = $this->config['fromName']; |
245
|
|
|
} else { |
246
|
|
|
$log->err('A from name must be provided (Variable $fromName) in Template: ' . $template); |
247
|
|
|
throw new \InvalidArgumentException('A from name must be provided (Variable $fromName) in Template: ' . $template); |
248
|
|
|
} |
249
|
|
View Code Duplication |
if (isset($this->config['subject'])) { |
|
|
|
|
250
|
|
|
$subject = $this->config['subject']; |
251
|
|
|
} else { |
252
|
|
|
$log->err('A subject must be provided (Variable $subject) in Template: ' . $template); |
253
|
|
|
throw new \InvalidArgumentException('A subject must be provided (Variable $subject) in Template: ' . $template); |
254
|
|
|
} |
255
|
|
|
$this->setFrom($from, $fromName); |
256
|
|
|
$this->setSubject($subject); |
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function send() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$log = $this->mailLogger; |
263
|
|
|
$this->getHeaders()->addHeaderLine('X-Mailer', 'php/YAWIK'); |
264
|
|
|
|
265
|
|
|
$this->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8'); |
266
|
|
|
|
267
|
|
|
$transport = $this->transport; |
268
|
|
|
$erg = false; |
269
|
|
|
try { |
270
|
|
|
$transport->send($this); |
271
|
|
|
$erg = true; |
272
|
|
|
$log->info($this); |
273
|
|
|
} catch (\Exception $e) { |
274
|
|
|
$log->err('Mail failure ' . $e->getMessage()); |
275
|
|
|
} |
276
|
|
|
return $erg; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param ContainerInterface $container |
281
|
|
|
* |
282
|
|
|
* @return static |
283
|
|
|
*/ |
284
|
|
|
public static function factory(ContainerInterface $container) |
285
|
|
|
{ |
286
|
|
|
//@TODO: need to define transport to be use during ::send() |
287
|
|
|
$mailLog = $container->get('Log/Core/Mail'); |
288
|
|
|
$viewResolver = $container->get('ViewResolver'); |
289
|
|
|
$eventManager = $container->get('EventManager'); |
290
|
|
|
$moduleManager = $container->get('ModuleManager'); |
291
|
|
|
return new static($mailLog,$viewResolver,$eventManager,$moduleManager); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.