1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2013 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2014-2022 |
7
|
|
|
* @package Base |
8
|
|
|
* @subpackage Mail |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\Base\Mail\Message; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Zend implementation for creating e-mails. |
17
|
|
|
* |
18
|
|
|
* @package Base |
19
|
|
|
* @subpackage Mail |
20
|
|
|
*/ |
21
|
|
|
class Typo3 implements \Aimeos\Base\Mail\Message\Iface |
22
|
|
|
{ |
23
|
|
|
private $charset; |
24
|
|
|
private $object; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes the message instance. |
29
|
|
|
* |
30
|
|
|
* @param \TYPO3\CMS\Core\Mail\MailMessage $object TYPO3 mail object |
31
|
|
|
* @param string $charset Default charset of the message |
32
|
|
|
*/ |
33
|
|
|
public function __construct( \TYPO3\CMS\Core\Mail\MailMessage $object, string $charset ) |
34
|
|
|
{ |
35
|
|
|
$this->charset = $charset; |
36
|
|
|
$this->object = $object; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds a source e-mail address of the message. |
42
|
|
|
* |
43
|
|
|
* @param string $email Source e-mail address |
44
|
|
|
* @param string|null $name Name of the user sending the e-mail or null for no name |
45
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
46
|
|
|
*/ |
47
|
|
|
public function from( string $email, string $name = null ) : Iface |
48
|
|
|
{ |
49
|
|
|
if( $email ) |
50
|
|
|
{ |
51
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
52
|
|
|
|
53
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
54
|
|
|
$this->object->addFrom( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
55
|
|
|
} else { |
56
|
|
|
$this->object->addFrom( $email, $name ); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds a destination e-mail address of the target user mailbox. |
66
|
|
|
* |
67
|
|
|
* @param string $email Destination address of the target mailbox |
68
|
|
|
* @param string|null $name Name of the user owning the target mailbox or null for no name |
69
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
70
|
|
|
*/ |
71
|
|
|
public function to( string $email, string $name = null ) : Iface |
72
|
|
|
{ |
73
|
|
|
if( $email ) |
74
|
|
|
{ |
75
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
76
|
|
|
|
77
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
78
|
|
|
$this->object->addTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
79
|
|
|
} else { |
80
|
|
|
$this->object->addTo( $email, $name ); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds a destination e-mail address for a copy of the message. |
90
|
|
|
* |
91
|
|
|
* @param string $email Destination address for a copy |
92
|
|
|
* @param string|null $name Name of the user owning the target mailbox or null for no name |
93
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
94
|
|
|
*/ |
95
|
|
|
public function cc( string $email, string $name = null ) : Iface |
96
|
|
|
{ |
97
|
|
|
if( $email ) |
98
|
|
|
{ |
99
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
100
|
|
|
|
101
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
102
|
|
|
$this->object->addCc( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
103
|
|
|
} else { |
104
|
|
|
$this->object->addCc( $email, $name ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Adds a destination e-mail address for a hidden copy of the message. |
114
|
|
|
* |
115
|
|
|
* @param array|string $email Destination address for a hidden copy |
116
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
117
|
|
|
*/ |
118
|
|
|
public function bcc( $email ) : Iface |
119
|
|
|
{ |
120
|
|
|
if( !empty( $email ) ) |
121
|
|
|
{ |
122
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
123
|
|
|
|
124
|
|
|
foreach( (array) $email as $addr ) |
125
|
|
|
{ |
126
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
127
|
|
|
$this->object->addBcc( new \Symfony\Component\Mime\Address( $addr ) ); |
128
|
|
|
} else { |
129
|
|
|
$this->object->addBcc( $addr ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Adds the return e-mail address for the message. |
140
|
|
|
* |
141
|
|
|
* @param string $email E-mail address which should receive all replies |
142
|
|
|
* @param string|null $name Name of the user which should receive all replies or null for no name |
143
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
144
|
|
|
*/ |
145
|
|
|
public function replyTo( string $email, string $name = null ) : Iface |
146
|
|
|
{ |
147
|
|
|
if( $email ) |
148
|
|
|
{ |
149
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
150
|
|
|
|
151
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
152
|
|
|
$this->object->addReplyTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
153
|
|
|
} else { |
154
|
|
|
$this->object->addReplyTo( $email, $name ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Adds a custom header to the message. |
164
|
|
|
* |
165
|
|
|
* @param string $name Name of the custom e-mail header |
166
|
|
|
* @param string $value Text content of the custom e-mail header |
167
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
168
|
|
|
*/ |
169
|
|
|
public function header( string $name, string $value ) : Iface |
170
|
|
|
{ |
171
|
|
|
if( $name ) |
172
|
|
|
{ |
173
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
174
|
|
|
|
175
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
176
|
|
|
$this->object->getHeaders()->add( new \Symfony\Component\Mime\Header\UnstructuredHeader( $name, $value ) ); |
177
|
|
|
} else { |
178
|
|
|
$this->object->getHeaders()->addTextHeader( $name, $value ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sends the e-mail message to the mail server. |
188
|
|
|
* |
189
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
190
|
|
|
*/ |
191
|
|
|
public function send() : Iface |
192
|
|
|
{ |
193
|
|
|
$this->object->send(); |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Sets the e-mail address and name of the sender of the message (higher precedence than "From"). |
200
|
|
|
* |
201
|
|
|
* @param string $email Source e-mail address |
202
|
|
|
* @param string|null $name Name of the user who sent the message or null for no name |
203
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
204
|
|
|
*/ |
205
|
|
|
public function sender( string $email, string $name = null ) : Iface |
206
|
|
|
{ |
207
|
|
|
if( $email ) |
208
|
|
|
{ |
209
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
210
|
|
|
|
211
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
212
|
|
|
$this->object->setSender( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
|
|
|
|
213
|
|
|
} else { |
214
|
|
|
$this->object->setSender( $email, $name ); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Sets the subject of the message. |
224
|
|
|
* |
225
|
|
|
* @param string $subject Subject of the message |
226
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
227
|
|
|
*/ |
228
|
|
|
public function subject( string $subject ) : Iface |
229
|
|
|
{ |
230
|
|
|
if( $subject ) |
231
|
|
|
{ |
232
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
233
|
|
|
|
234
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
235
|
|
|
$this->object->setSubject( $subject ); |
236
|
|
|
} else { |
237
|
|
|
$this->object->setSubject( $subject ); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Sets the text body of the message. |
247
|
|
|
* |
248
|
|
|
* @param string $message Text body of the message |
249
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
250
|
|
|
*/ |
251
|
|
|
public function text( string $message ) : Iface |
252
|
|
|
{ |
253
|
|
|
if( $message ) |
254
|
|
|
{ |
255
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
256
|
|
|
|
257
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
258
|
|
|
$this->object->text( $message, $this->charset ); |
259
|
|
|
} elseif( class_exists( '\Swift_Mailer' ) ) { |
260
|
|
|
$this->object->setBody( $message ); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Sets the HTML body of the message. |
270
|
|
|
* |
271
|
|
|
* @param string $message HTML body of the message |
272
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
273
|
|
|
*/ |
274
|
|
|
public function html( string $message ) : Iface |
275
|
|
|
{ |
276
|
|
|
if( $message ) |
277
|
|
|
{ |
278
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
279
|
|
|
|
280
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) { |
281
|
|
|
$this->object->html( $message, $this->charset ); |
282
|
|
|
} elseif( class_exists( '\Swift_Mailer' ) ) { |
283
|
|
|
$this->object->addPart( $message, 'text/html' ); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Adds an attachment to the message. |
293
|
|
|
* |
294
|
|
|
* @param string $data Binary or string |
295
|
|
|
* @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
296
|
|
|
* @param string|null $filename Name of the attached file (or null if inline disposition is used) |
297
|
|
|
* @param string $disposition Type of the disposition ("attachment" or "inline") |
298
|
|
|
* @return \Aimeos\Base\Mail\Message\Iface Message object |
299
|
|
|
*/ |
300
|
|
|
public function attach( string $data, string $mimetype, string $filename, string $disposition = 'attachment' ) : Iface |
301
|
|
|
{ |
302
|
|
|
if( $data ) |
303
|
|
|
{ |
304
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
305
|
|
|
|
306
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) |
307
|
|
|
{ |
308
|
|
|
$this->object->attach( $data, $filename, $mimetype ); |
309
|
|
|
} |
310
|
|
|
elseif( class_exists( '\Swift_Attachment' ) ) |
311
|
|
|
{ |
312
|
|
|
$part = \Swift_Attachment::newInstance( $data, $filename, $mimetype ); |
|
|
|
|
313
|
|
|
$part->setDisposition( $disposition ); |
314
|
|
|
$this->object->attach( $part ); |
315
|
|
|
} |
316
|
|
|
else |
317
|
|
|
{ |
318
|
|
|
throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' ); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Embeds an attachment into the message and returns its reference. |
328
|
|
|
* |
329
|
|
|
* @param string $data Binary or string |
330
|
|
|
* @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
331
|
|
|
* @param string|null $filename Name of the attached file |
332
|
|
|
* @return string Content ID for referencing the attachment in the HTML body |
333
|
|
|
*/ |
334
|
|
|
public function embed( string $data, string $mimetype, string $filename ) : string |
335
|
|
|
{ |
336
|
|
|
$class = '\Symfony\Component\Mime\Email'; |
337
|
|
|
|
338
|
|
|
if( !$filename ) { |
339
|
|
|
$filename = md5( $data ); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if( class_exists( $class ) && $this->object instanceof $class ) |
343
|
|
|
{ |
344
|
|
|
$this->object->embed( $data, $filename, $mimetype ); |
345
|
|
|
return 'cid:' . $filename; |
346
|
|
|
} |
347
|
|
|
elseif( class_exists( '\Swift_EmbeddedFile' ) ) |
348
|
|
|
{ |
349
|
|
|
$part = \Swift_EmbeddedFile::newInstance( $data, $filename, $mimetype ); |
|
|
|
|
350
|
|
|
return $this->object->embed( $part ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' ); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns the internal TYPO3 mail message object. |
359
|
|
|
* |
360
|
|
|
* @return \TYPO3\CMS\Core\Mail\MailMessage TYPO3 mail message object |
361
|
|
|
*/ |
362
|
|
|
public function object() : \TYPO3\CMS\Core\Mail\MailMessage |
363
|
|
|
{ |
364
|
|
|
return $this->object; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Clones the internal objects. |
370
|
|
|
*/ |
371
|
|
|
public function __clone() |
372
|
|
|
{ |
373
|
|
|
$this->object = clone $this->object; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|