|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk/) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/) |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WebinoDev\Mail; |
|
11
|
|
|
|
|
12
|
|
|
use eXorus\PhpMimeMailParser\Parser; |
|
13
|
|
|
use Zend\Mail\Headers; |
|
14
|
|
|
use Zend\Mail\Message as BaseMessage; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Extended Zend's mail message with attachments support |
|
18
|
|
|
*/ |
|
19
|
|
|
class Message extends BaseMessage |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Parser |
|
23
|
|
|
*/ |
|
24
|
|
|
private $parser; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Return mail parser |
|
28
|
|
|
* |
|
29
|
|
|
* @return Parser |
|
30
|
|
|
*/ |
|
31
|
|
|
private function getParser() |
|
32
|
|
|
{ |
|
33
|
|
|
if (null === $this->parser) { |
|
34
|
|
|
$this->parser = new Parser; |
|
35
|
|
|
$this->fixUniqueHeaders(); |
|
36
|
|
|
$this->parser->setText($this->toString()); |
|
37
|
|
|
} |
|
38
|
|
|
return $this->parser; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Return mail sent date |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getDate() |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var \Zend\Mail\Header\Date $date */ |
|
49
|
|
|
$date = $this->getHeaders()->get('Date'); |
|
50
|
|
|
return $date->getFieldValue(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return the message body |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getBody() |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->getAttachments()) { |
|
61
|
|
|
try { |
|
62
|
|
|
return $this->getParser()->getMessageBody('html'); |
|
63
|
|
|
} catch (\Throwable $exc) { |
|
64
|
|
|
error_log($exc); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
return parent::getBody(); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return array of mail attachments |
|
72
|
|
|
* |
|
73
|
|
|
* @return \eXorus\PhpMimeMailParser\Attachment[] |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getAttachments() |
|
76
|
|
|
{ |
|
77
|
|
|
/** @var \eXorus\PhpMimeMailParser\Attachment[] $attachments */ |
|
78
|
|
|
$attachments = $this->getParser()->getAttachments(); |
|
79
|
|
|
return $attachments; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Fixing headers uniqueness |
|
84
|
|
|
*/ |
|
85
|
|
|
private function fixUniqueHeaders() |
|
86
|
|
|
{ |
|
87
|
|
|
$headers = $this->getHeaders(); |
|
88
|
|
|
$headersCopy = clone $headers; |
|
89
|
|
|
$headers->clearHeaders(); |
|
90
|
|
|
foreach ($headersCopy as $header) { |
|
91
|
|
|
$name = $header->getFieldName(); |
|
92
|
|
|
$headers->has($name) or $headers->addHeader($header); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create mail object from file |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $path Mail file path |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function fromFile($path) |
|
103
|
|
|
{ |
|
104
|
|
|
/** @var self $message */ |
|
105
|
|
|
$message = static::fromString(join(Headers::EOL, file($path, FILE_IGNORE_NEW_LINES))); |
|
106
|
|
|
return $message; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|