forked from clime/pimcore-custom
/
Mail.php
664 lines (575 loc) · 19.8 KB
/
Mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
/**
* Pimcore
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.pimcore.org/license
*
* @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
* @license http://www.pimcore.org/license New BSD License
*/
class Pimcore_Mail extends Zend_Mail
{
/**
* Contains the debug email addresses from settings -> system -> Email Settings -> Debug email addresses
*
* @var array
* @static
*/
protected static $debugEmailAddresses = array();
/**
* @var object Pimcore_Placeholder
*/
protected $placeholderObject;
/**
* Contains data that has to be stored temporary e.g. email receivers for logging
*
* @var array
*/
protected $temporaryStorage = array();
/**
* If true - emails are logged in the database and on the file-system
*
* @var bool
*/
protected $loggingEnable = true;
/**
* Contains the email document
*
* @var object Document_Email
*/
protected $document;
/**
* Contains the dynamic Params for the Placeholders
*
* @var array
*/
protected $params = array();
/**
* Prevent adding debug information
*
* @var bool
*/
protected $preventDebugInformationAppending = false;
/**
* if true - the Pimcore debug mode is ignored
* @var bool
*/
protected $ignoreDebugMode = false;
/**
* if true - the layout is enabled when document is rendered to a string
* @var bool
*/
protected $enableLayoutOnPlaceholderRendering = true;
/**
* Creates a new Pimcore_Mail object (extends Zend_Mail)
*
* @param array $options
*/
public function __construct($charset = null)
{
// using $charset as param to be compatible with Zend_Mail
if(is_array($charset)) {
$options = $charset;
parent::__construct($options["charset"] ? $options["charset"] : "UTF-8");
if ($options["document"]) {
$this->setDocument($options["document"]);
}
if ($options['params']) {
$this->setParams($options['params']);
}
if ($options['subject']) {
$this->setSubject($options['subject']);
}
} else {
parent::__construct($charset);
}
$this->init();
}
/**
* Initializes the mailer with the settings form Settings -> System -> Email Settings
*
* @return void
*/
protected function init()
{
$systemConfig = Pimcore_Config::getSystemConfig()->toArray();
$emailSettings =& $systemConfig['email'];
if ($emailSettings['sender']['email']) {
$this->setDefaultFrom($emailSettings['sender']['email'], $emailSettings['sender']['name']);
}
if ($emailSettings['return']['email']) {
$this->setDefaultReplyTo($emailSettings['return']['email'], $emailSettings['return']['name']);
}
if ($emailSettings['method'] == "smtp") {
$config = array();
if ($emailSettings['smtp']['name']) {
$config['name'] = $emailSettings['smtp']['name'];
}
if ($emailSettings['smtp']['ssl']) {
$config['ssl'] = $emailSettings['smtp']['ssl'];
}
if ($emailSettings['smtp']['port']) {
$config['port'] = $emailSettings['smtp']['port'];
}
if ($emailSettings['smtp']['auth']['method']) {
$config['auth'] = $emailSettings['smtp']['auth']['method'];
$config['username'] = $emailSettings['smtp']['auth']['username'];
$config['password'] = $emailSettings['smtp']['auth']['password'];
}
$transport = new Zend_Mail_Transport_Smtp($emailSettings['smtp']['host'], $config);
$this->setDefaultTransport($transport);
}
//setting debug email addresses
if (empty(self::$debugEmailAddresses)) {
if ($emailSettings['debug']['emailaddresses']) {
foreach (explode(',', $emailSettings['debug']['emailaddresses']) as $emailAddress) {
self::$debugEmailAddresses[] = $emailAddress;
}
}
}
$this->placeholderObject = new Pimcore_Placeholder();
}
/**
* To ignore the Pimcore debug mode
*
* @param bool $value
*/
public function setIgnoreDebugMode($value){
$this->ignoreDebugMode = (bool)$value;
}
/**
* Checks if the Debug mode is ignored
*
* @return bool
*/
public function getIgnoreDebugMode(){
return $this->ignoreDebugMode;
}
/**
* activate / deactivate the layout when the document is rendered
* to a string when the placeholders are replaced
*
* @param $value bool
*/
public function setEnableLayoutOnPlaceholderRendering($value){
$this->enableLayoutOnPlaceholderRendering = (bool)$value;
}
/**
* @return bool
*/
public function getEnableLayoutOnPlaceholderRendering(){
return $this->enableLayoutOnPlaceholderRendering;
}
// overwriting Zend_Mail methods - necessary for logging... - start
/**
* Adds To-header and recipient, $email can be an array, or a single string address
* Additionally adds recipients to temporary storage
*
* @param string|array $email
* @param string $name
* @return Pimcore_Mail Provides fluent interface
*/
public function addTo($email, $name = '')
{
$this->addToTemporaryStorage('To', $email, $name);
return parent::addTo($email, $name);
}
/**
* Adds Cc-header and recipient, $email can be an array, or a single string address
* Additionally adds recipients to temporary storage
*
* @param string|array $email
* @param string $name
* @return Pimcore_Mail Provides fluent interface
*/
public function addCc($email, $name = '')
{
$this->addToTemporaryStorage('Cc', $email, $name);
return parent::addCc($email, $name);
}
/**
* Adds Bcc recipient, $email can be an array, or a single string address
* Additionally adds recipients to temporary storage
*
* @param string|array $email
* @return Pimcore_Mail Provides fluent interface
*/
public function addBcc($email)
{
$this->addToTemporaryStorage('Bcc', $email, '');
return parent::addBcc($email);
}
/**
* Clears list of recipient email addresses
* and resets the temporary storage
*
* @return Pimcore_Mail Provides fluent interface
*/
public function clearRecipients()
{
unset($this->temporaryStorage['To']);
unset($this->temporaryStorage['Cc']);
unset($this->temporaryStorage['Bcc']);
return parent::clearRecipients();
}
// overwriting Zend_Mail methods - end
/**
* Helper to add receivers to the temporary storage
*
* @param string $key
* @param string | array $email
* @param string $name
*/
protected function addToTemporaryStorage($key, $email, $name)
{
if (!is_array($email)) {
$email = array($name => $email);
}
foreach ($email as $n => $recipient) {
$this->temporaryStorage[$key][] = array('email' => $recipient, 'name' => is_int($n) ? '' : $n);
}
}
/**
* Returns the temporary storage
*
* @return array
*/
public function getTemporaryStorage()
{
return $this->temporaryStorage;
}
/**
* Disables email logging
*
* @return Pimcore_Mail Provides fluent interface
*/
public function disableLogging()
{
$this->loggingEnable = false;
return $this;
}
/**
* Enables email logging (by default it's enabled)
*
* @return Pimcore_Mail Provides fluent interface
*/
public function enableLogging()
{
$this->loggingEnable = true;
return $this;
}
/**
* returns the logging status
*
* @return bool
*/
public function loggingIsEnabled()
{
return $this->loggingEnable;
}
/**
* Sets the parameters for the email view and the Placeholders
*
* @param array $params
* @return Pimcore_Mail Provides fluent interface
*/
public function setParams(Array $params)
{
foreach ($params as $key => $value) {
$this->setParam($key, $value);
}
return $this;
}
/**
* Sets a single parameter for the email view and the Placeholders
*
* @param string | int $key
* @param mixed $value
* @return Pimcore_Mail Provides fluent interface
*/
public function setParam($key, $value)
{
if (is_string($key) || is_integer($key)) {
$this->params[$key] = $value;
} else {
Logger::warn('$key has to be a string - Param ignored!');
}
return $this;
}
/**
* Returns the parameters which were set with "setParams" or "setParam"
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Returns a parameter which was set with "setParams" or "setParam"
*
* @param string | integer $key
* @return mixed
*/
public function getParam($key)
{
return $this->params[$key];
}
/**
* Deletes parameters which were set with "setParams" or "setParam"
*
* @param array $params
* @return Pimcore_Mail Provides fluent interface
*/
public function unsetParams(Array $params)
{
foreach ($params as $param) {
$this->unsetParam($param);
}
return $this;
}
/**
* Deletes a single parameter which was set with "setParams" or "setParam"
*
* @param string | integer $key
* @return Pimcore_Mail Provides fluent interface
*/
public function unsetParam($key)
{
if (is_string($key) || is_integer($key)) {
unset($this->params[$key]);
} else {
Logger::warn('$key has to be a string - unsetParam ignored!');
}
return $this;
}
/**
* Sets the settings which are defined in the Document Settings (from,to,cc,bcc)
*
* @return Pimcore_Mail Provides fluent interface
*/
protected function setDocumentSettings()
{
$document = $this->getDocument();
if ($document instanceof Document_Email) {
$to = $document->getToAsArray();
if (!empty($to)) {
$this->addTo($to);
}
$cc = $document->getCcAsArray();
if (!empty($cc)) {
$this->addCc($cc);
}
$bcc = $document->getBccAsArray();
if (!empty($bcc)) {
$this->addBcc($bcc);
}
//if more than one "from" email address is defined -> we set the first one
list($from) = $document->getFromAsArray();
if ($from) {
$this->clearFrom();
$this->setFrom($from);
}
}
return $this;
}
/**
* Sends this email using the given transport or with the settings from "Settings" -> "System" -> "Email Settings"
*
* IMPORTANT: If the debug mode is enabled in "Settings" -> "System" -> "Debug" all emails will be sent to the
* debug email addresses that are given in "Settings" -> "System" -> "Email Settings" -> "Debug email addresses"
*
* set DefaultTransport or the internal mail function if no
* default transport had been set.
*
* @param Zend_Mail_Transport_Abstract $transport
* @return Pimcore_Mail Provides fluent interface
*/
public function send($transport = null)
{
if ($this->getDocument()) {
$this->setDocumentSettings();
$this->setSubject($this->getSubjectRendered());
$this->setBodyHtml($this->getBodyHtmlRendered());
$this->setBodyText($this->getBodyTextRendered());
}
if($this->ignoreDebugMode == false){
$this->checkDebugMode();
}
$result = parent::send($transport);
if ($this->loggingIsEnabled() && $this->getDocument()) {
try {
Pimcore_Helper_Mail::logEmail($this);
} catch (Exception $e) {
Logger::emerg("Couldn't log Email");
}
}
return $result;
}
/**
* Checks if the debug mode is enabled in "Settings" -> "System" -> "Debug"
* If the debug mode is enabled, all emails will be sent to the debug email addresses given the system settings
* and the debug information is appended
*
* @return void
*/
protected function checkDebugMode()
{
if (Pimcore::inDebugMode()) {
if (empty(self::$debugEmailAddresses)) {
throw new Exception('No valid debug email address given in "Settings" -> "System" -> "Email Settings"');
}
if($this->preventDebugInformationAppending != true){
//adding the debug information to the html email
$html = $this->getBodyHtml();
if ($html instanceof Zend_Mime_Part) {
$rawHtml = $html->getRawContent();
$debugInformation = Pimcore_Helper_Mail::getDebugInformation('html', $this);
$debugInformationStyling = Pimcore_Helper_Mail::getDebugInformationCssStyle();
$rawHtml = preg_replace("!(</\s*body\s*>)!is", "$debugInformation\\1", $rawHtml);
$rawHtml = preg_replace("!(<\s*head\s*>)!is", "\\1$debugInformationStyling", $rawHtml);
$this->setBodyHtml($rawHtml);
}
$text = $this->getBodyText();
if($text instanceof Zend_Mime_Part){
$rawText = $text->getRawContent();
$debugInformation = Pimcore_Helper_Mail::getDebugInformation('text',$this);
$rawText .= $debugInformation;
$this->setBodyText($rawText);
}
//setting debug subject
$subject = $this->getSubject();
$this->clearSubject();
$this->setSubject('Debug email: ' . $subject);
}
$this->clearRecipients();
$this->addTo(self::$debugEmailAddresses);
}
}
/**
* Static helper to validate a email address
*
* @static
* @param $emailAddress
* @return bool
*/
public static function isValidEmailAddress($emailAddress)
{
$validator = new Zend_Validate_EmailAddress();
return $validator->isValid($emailAddress);
}
/**
* Replaces the placeholders with the content and returns the rendered Subject
*
* @return string
*/
public function getSubjectRendered()
{
$subject = $this->getSubject();
$this->clearSubject();
if (!$subject && $this->getDocument()) {
$subject = $this->getDocument()->getSubject();
}
return $this->placeholderObject->replacePlaceholders($subject, $this->getParams(), $this->getDocument(),$this->getEnableLayoutOnPlaceholderRendering());
}
/**
* Replaces the placeholders with the content and returns the rendered Html
*
* @return string|null
*/
public function getBodyHtmlRendered()
{
$html = $this->getBodyHtml();
//if the content was manually set with $obj->setBodyHtml(); this content will be used
//and not the content of the Document!
if ($html instanceof Zend_Mime_Part) {
$rawHtml = $html->getRawContent();
$content = $this->placeholderObject->replacePlaceholders($rawHtml, $this->getParams(), $this->getDocument(),$this->getEnableLayoutOnPlaceholderRendering());
} elseif ($this->getDocument() instanceof Document) {
$content = $this->placeholderObject->replacePlaceholders($this->getDocument(), $this->getParams(), $this->getDocument(),$this->getEnableLayoutOnPlaceholderRendering());
} else {
$content = null;
}
//modifying the content e.g set absolute urls...
if ($content) {
$content = Pimcore_Helper_Mail::embedAndModifyCss($content, $this->getDocument());
$content = Pimcore_Helper_Mail::setAbsolutePaths($content, $this->getDocument());
}
return $content;
}
/**
* Replaces the placeholders with the content and returns
* the rendered text if a text was set with "$mail->setBodyText()" *
* @return string
*/
public function getBodyTextRendered()
{
$text = $this->getBodyText();
//if the content was manually set with $obj->setBodyText(); this content will be used
if ($text instanceof Zend_Mime_Part) {
$rawText = $text->getRawContent();
$content = $this->placeholderObject->replacePlaceholders($rawText, $this->getParams(), $this->getDocument(),$this->getEnableLayoutOnPlaceholderRendering());
} else {
//creating text version from html email if html2text is installed
try {
include_once("simple_html_dom.php");
include_once("html2text.php");
$htmlContent = $this->getBodyHtmlRendered();
$html = str_get_html($htmlContent);
if($html) {
$body = $html->find("body",0);
if($body) {
$htmlContent = $body->innertext;
}
}
$content = @html2text($htmlContent);
} catch (Exception $e) {
Logger::err($e);
$content = "";
}
}
return $content;
}
/**
* Sets the email document
*
* @param Document_Email $document
* @throws Exception
*/
public function setDocument($document)
{
if ($document instanceof Document) { //document passed
$this->document = $document;
} elseif ((int)$document > 0) { //id of document passed
$this->setDocument(Document::getById($document));
} elseif (is_string($document) && $document != "") { //path of document passed
$this->setDocument(Document::getByPath($document));
} else {
throw new Exception('$document is not an instance of Document_Email or at least Document');
}
return $this;
}
/**
* Returns the Document
*
* @return Document_Email | null
*/
public function getDocument()
{
return $this->document;
}
/**
* Prevents appending of debug information (used for resending emails)
*
* @return Pimcore_Mail
*/
public function preventDebugInformationAppending(){
$this->preventDebugInformationAppending = true;
return $this;
}
}