1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Core\Mail; |
12
|
|
|
|
13
|
|
|
use Core\Factory\ContainerAwareInterface; |
14
|
|
|
use Interop\Container\ContainerInterface; |
15
|
|
|
use Zend\I18n\Translator\TranslatorAwareInterface; |
16
|
|
|
use Zend\Mail\Address; |
17
|
|
|
use Zend\Mail\AddressList; |
18
|
|
|
use Zend\Mail\Message as MailMessage; |
19
|
|
|
use Zend\Mail\Transport\TransportInterface; |
20
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
21
|
|
|
use Zend\ServiceManager\ConfigInterface; |
22
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Mail Plugin Manager |
26
|
|
|
* |
27
|
|
|
* @author Mathias Gelhausen <[email protected]> |
28
|
|
|
* @author Mathias Weitz <[email protected]> |
29
|
|
|
* @author Carsten Bleek <[email protected]> |
30
|
|
|
* @author Anthonius Munthi <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class MailService extends AbstractPluginManager |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Define transport type to use |
36
|
|
|
*/ |
37
|
|
|
const TRANSPORT_SMTP = 'smtp'; |
38
|
|
|
const TRANSPORT_FILE = 'file'; |
39
|
|
|
const TRANSPORT_SENDMAIL = 'sendmail'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The mail Transport |
43
|
|
|
* |
44
|
|
|
* @var TransportInterface |
45
|
|
|
*/ |
46
|
|
|
protected $transport; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Default from address to use if no from address is set in the mail. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $from; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Value for the X-Mailer header. |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $mailer; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If set, all mails are send to the addresses in this list |
64
|
|
|
* |
65
|
|
|
* This is useful when developing. |
66
|
|
|
* |
67
|
|
|
* @var AddressList|null |
68
|
|
|
*/ |
69
|
|
|
protected $overrideRecipient; |
70
|
|
|
|
71
|
|
|
protected $language; |
72
|
|
|
|
73
|
|
|
protected $shareByDefault = false; |
74
|
|
|
|
75
|
|
|
protected $invokableClasses = array( |
76
|
|
|
'simple' => '\Zend\Mail\Message', |
77
|
|
|
'stringtemplate' => '\Core\Mail\StringTemplateMessage', |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
protected $factories = array( |
81
|
|
|
'htmltemplate' => [HTMLTemplateMessage::class,'factory'], |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates an instance. |
86
|
|
|
* |
87
|
|
|
* Adds two default initializers: |
88
|
|
|
* - Inject the translator to mails implementing TranslatorAwareInterface |
89
|
|
|
* - Call init() method on Mails if such method exists. |
90
|
|
|
* |
91
|
|
|
* @param ContainerInterface $container |
92
|
|
|
* @param mixed $configuration |
93
|
|
|
*/ |
94
|
17 |
|
public function __construct($container, $configuration = []) |
95
|
|
|
{ |
96
|
17 |
|
parent::__construct($container, $configuration); |
97
|
|
|
|
98
|
17 |
|
$this->addInitializer( |
99
|
|
|
function ($context, $instance) { |
100
|
1 |
|
if ($instance instanceof TranslatorAwareInterface) { |
101
|
1 |
|
$translator = $context->get('translator'); |
102
|
1 |
|
$instance->setTranslator($translator); |
103
|
1 |
|
if (null === $instance->getTranslatorTextDomain()) { |
|
|
|
|
104
|
1 |
|
$instance->setTranslatorTextDomain(); |
105
|
|
|
} |
106
|
1 |
|
$instance->setTranslatorEnabled(true); |
107
|
|
|
} |
108
|
1 |
|
if ($instance instanceof ContainerAwareInterface) { |
109
|
|
|
$instance->setContainer($context); |
110
|
|
|
} |
111
|
17 |
|
} |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
//@TODO: [ZF3] verify that removing this lines is save |
115
|
|
|
//$this->addInitializer( |
116
|
|
|
// function ($context,$instance) { |
117
|
|
|
// if (method_exists($instance, 'setServiceLocator')) { |
118
|
|
|
// //$instance->setServiceLocator($this); |
119
|
|
|
// } |
120
|
|
|
// } |
121
|
|
|
//); |
122
|
|
|
|
123
|
17 |
|
$this->addInitializer( |
124
|
|
|
function ($context, $instance) { |
125
|
1 |
|
if (method_exists($instance, 'init')) { |
126
|
1 |
|
$instance->init(); |
127
|
|
|
} |
128
|
17 |
|
} |
129
|
|
|
); |
130
|
17 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Checks that a plugin is a child of an email message. |
134
|
|
|
* |
135
|
|
|
* @throws \InvalidArgumentException |
136
|
|
|
*/ |
137
|
3 |
|
public function validate($plugin) |
138
|
|
|
{ |
139
|
3 |
|
if (!$plugin instanceof MailMessage) { |
140
|
1 |
|
throw new \InvalidArgumentException( |
141
|
1 |
|
sprintf( |
142
|
1 |
|
'Expected instance of \Zend\Mail\Message but received %s', |
143
|
1 |
|
get_class($plugin) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
3 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Gets the default from address |
151
|
|
|
* |
152
|
|
|
* @return null|String|Address|AddressList|array |
153
|
|
|
*/ |
154
|
5 |
|
public function getFrom() |
155
|
|
|
{ |
156
|
5 |
|
return $this->from; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets the default from address. |
161
|
|
|
* |
162
|
|
|
* @param string|AddressList|Address $email |
163
|
|
|
* @param String|null $name |
164
|
|
|
* |
165
|
|
|
* @return self |
166
|
|
|
*/ |
167
|
9 |
|
public function setFrom($email, $name = null) |
168
|
|
|
{ |
169
|
9 |
|
if (is_array($email)) { |
|
|
|
|
170
|
2 |
|
$this->from = [$email['email'] => $email['name']]; |
171
|
|
|
} else { |
172
|
9 |
|
$this->from = is_object($email) || null === $name |
173
|
8 |
|
? $email |
174
|
1 |
|
: array($email => $name); |
175
|
|
|
} |
176
|
|
|
|
177
|
9 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Sets override recipients. |
182
|
|
|
* |
183
|
|
|
* @param AddressList $recipients |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
2 |
|
public function setOverrideRecipient(AddressList $recipients) |
188
|
|
|
{ |
189
|
2 |
|
$this->overrideRecipient = $recipients; |
190
|
|
|
|
191
|
2 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Sends a mail. |
196
|
|
|
* |
197
|
|
|
* Sets default values where needed. |
198
|
|
|
* |
199
|
|
|
* @param string|MailMessage $mail |
200
|
|
|
* @param array $options |
201
|
|
|
*/ |
202
|
5 |
|
public function send($mail, array $options = array()) |
203
|
|
|
{ |
204
|
5 |
|
if (!$mail instanceof MailMessage) { |
205
|
1 |
|
$mail = $this->get($mail, $options); |
206
|
|
|
} |
207
|
|
|
|
208
|
5 |
|
$headers = $mail->getHeaders(); |
209
|
5 |
|
$transport = $this->getTransport(); |
210
|
|
|
|
211
|
5 |
|
if (!$mail->isValid() && $this->from) { |
212
|
1 |
|
$mail->setFrom($this->from); |
213
|
|
|
} |
214
|
|
|
|
215
|
5 |
|
if ($this->overrideRecipient instanceof AddressList) { |
216
|
1 |
|
$originalRecipient = $headers->get('to')->toString(); |
|
|
|
|
217
|
1 |
|
if ($headers->has('cc')) { |
218
|
1 |
|
$originalRecipient .= '; ' . $headers->get('cc')->toString(); |
219
|
1 |
|
$headers->removeHeader('cc'); |
220
|
|
|
} |
221
|
1 |
|
if ($headers->has('bcc')) { |
222
|
1 |
|
$originalRecipient .= '; ' . $headers->get('bcc')->toString(); |
223
|
1 |
|
$headers->removeHeader('bcc'); |
224
|
|
|
} |
225
|
1 |
|
$headers->addHeaderLine('X-Original-Recipients', $originalRecipient); |
226
|
1 |
|
$mail->setTo($this->overrideRecipient); |
227
|
|
|
} |
228
|
|
|
|
229
|
5 |
|
if (!$headers->has('X-Mailer')) { |
230
|
5 |
|
$mailerHeader = new \Zend\Mail\Header\GenericHeader('X-Mailer', $this->getMailer()); |
231
|
5 |
|
$headers->addHeader($mailerHeader); |
232
|
5 |
|
$mailerHeader->setEncoding('ASCII'); // get rid of other encodings for this header! |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/* Allow HTMLTemplateMails to alter subject in the view script. |
236
|
|
|
* As the Zend Transport class build subject before the getBodyText call, |
237
|
|
|
* we have to call it here. |
238
|
|
|
*/ |
239
|
5 |
|
if ($mail instanceof \Core\Mail\HTMLTemplateMessage) { |
240
|
|
|
$mail->getBodyText(); |
241
|
|
|
} |
242
|
|
|
|
243
|
5 |
|
$transport->send($mail); |
244
|
5 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Gets the transport. |
248
|
|
|
* |
249
|
|
|
* @return TransportInterface $transport |
250
|
|
|
*/ |
251
|
6 |
|
public function getTransport() |
252
|
|
|
{ |
253
|
6 |
|
return $this->transport; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Sets the transport |
258
|
|
|
* |
259
|
|
|
* @param TransportInterface $transport |
260
|
|
|
* |
261
|
|
|
* @return self |
262
|
|
|
*/ |
263
|
8 |
|
public function setTransport(TransportInterface $transport) |
264
|
|
|
{ |
265
|
8 |
|
$this->transport = $transport; |
266
|
|
|
|
267
|
8 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Gest the value of the X-Mailer header. |
272
|
|
|
* |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
6 |
|
public function getMailer() |
276
|
|
|
{ |
277
|
6 |
|
return $this->mailer; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Sets the value for the X-Mailer header |
282
|
|
|
* |
283
|
|
|
* @param string $mailer |
284
|
|
|
* |
285
|
|
|
* @return $this |
286
|
|
|
*/ |
287
|
4 |
|
public function setMailer($mailer) |
288
|
|
|
{ |
289
|
4 |
|
$this->mailer = $mailer; |
290
|
|
|
|
291
|
4 |
|
return $this; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|