1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package org.openpsa.mail |
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Mime\Email; |
10
|
|
|
use Symfony\Component\Mime\Part\DataPart; |
11
|
|
|
use Symfony\Component\Mime\MimeTypes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Wrapper class for emails |
15
|
|
|
* |
16
|
|
|
* @package org.openpsa.mail |
17
|
|
|
*/ |
18
|
|
|
class org_openpsa_mail_message |
19
|
|
|
{ |
20
|
|
|
private $_to; |
21
|
|
|
|
22
|
|
|
private $_encoding; |
23
|
|
|
|
24
|
|
|
private $_headers; |
25
|
|
|
|
26
|
|
|
private $_body; |
27
|
|
|
private $_html_body; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Email |
31
|
|
|
*/ |
32
|
|
|
private $_message; |
33
|
|
|
|
34
|
8 |
|
public function __construct($to, array $headers, string $encoding) |
35
|
|
|
{ |
36
|
8 |
|
$this->_to = $to; |
37
|
8 |
|
$this->_headers = $headers; |
38
|
8 |
|
$this->_encoding = $encoding; |
39
|
|
|
|
40
|
8 |
|
$this->_message = new Email; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
public function get_recipients() |
44
|
|
|
{ |
45
|
8 |
|
return $this->_to; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function get_message() : Email |
49
|
|
|
{ |
50
|
|
|
// set headers |
51
|
|
|
$headers_setter_map = [ |
52
|
|
|
"from" => "from", |
53
|
|
|
"to" => "to", |
54
|
|
|
"cc" => "cc", |
55
|
|
|
"bcc" => "bcc", |
56
|
|
|
"reply-to" => "replyTo", |
57
|
|
|
"subject" => "subject", |
58
|
|
|
"date" => "date", |
59
|
|
|
"return-path" => "returnPath" |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
// map headers we got to sf mailer setter methods |
63
|
|
|
$msg_headers = $this->_message->getHeaders(); |
64
|
|
|
|
65
|
|
|
foreach ($this->get_headers() as $name => $value) { |
66
|
|
|
if (array_key_exists(strtolower($name), $headers_setter_map)) { |
67
|
|
|
$setter = $headers_setter_map[strtolower($name)]; |
68
|
|
|
if (is_array($value)) { |
69
|
|
|
$this->_message->$setter(...$value); |
70
|
|
|
} else { |
71
|
|
|
$this->_message->$setter($value); |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$msg_headers->addHeader($name, $value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// somehow we need to set the body after the headers... |
79
|
|
|
if (!empty($this->_html_body)) { |
80
|
|
|
$this->_message->html($this->_html_body); |
81
|
|
|
} |
82
|
|
|
$this->_message->text($this->_body); |
83
|
|
|
|
84
|
|
|
return $this->_message; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function set_header_field(string $name, $value) |
88
|
|
|
{ |
89
|
|
|
$this->_headers[$name] = $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
public function get_headers() : array |
93
|
|
|
{ |
94
|
8 |
|
reset($this->_headers); |
95
|
8 |
|
foreach ($this->_headers as $header => $value) { |
96
|
8 |
|
if (is_string($value)) { |
97
|
8 |
|
$this->_headers[$header] = trim($value); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
8 |
|
return $this->_headers; |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
public function get_body() |
105
|
|
|
{ |
106
|
8 |
|
return $this->_body; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
public function set_body($body) |
110
|
|
|
{ |
111
|
6 |
|
$this->_body = $body; |
112
|
6 |
|
$this->_html_body = null; |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function set_html_body(string $body, string $altBody, array $attachments, bool $do_image_embedding) |
116
|
|
|
{ |
117
|
2 |
|
$this->_body = $altBody; |
118
|
2 |
|
$this->_html_body = $body; |
119
|
|
|
|
120
|
|
|
// adjust html body |
121
|
2 |
|
if ($do_image_embedding) { |
122
|
1 |
|
$this->_embed_images(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// process attachments |
126
|
2 |
|
$this->_process_attachments($attachments); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
private function _embed_images() |
130
|
|
|
{ |
131
|
|
|
// anything with SRC = "" something in it (images etc) |
132
|
1 |
|
$regExp_src = "/(src|background)=([\"'�])(((https?|ftp):\/\/)?(.*?))\\2/i"; |
133
|
1 |
|
preg_match_all($regExp_src, $this->_html_body, $matches_src); |
134
|
1 |
|
debug_print_r("matches_src:", $matches_src); |
135
|
|
|
|
136
|
1 |
|
$matches = [ |
137
|
1 |
|
"whole" => $matches_src[0], |
138
|
1 |
|
"uri" => $matches_src[3], |
139
|
1 |
|
"proto" => $matches_src[4], |
140
|
1 |
|
"location" => $matches_src[6] |
141
|
|
|
]; |
142
|
|
|
|
143
|
1 |
|
foreach ($matches["whole"] as $key => $match) { |
144
|
|
|
$location = $matches["location"][$key]; |
145
|
|
|
// uri is fully qualified |
146
|
|
|
if ($matches['proto'][$key]) { |
147
|
|
|
$uri = $matches["uri"][$key]; |
148
|
|
|
} |
149
|
|
|
// uri is absolute |
150
|
|
|
elseif (str_starts_with($location, '/')) { |
151
|
|
|
$uri = midcom::get()->get_host_name() . $location; |
152
|
|
|
} else { |
153
|
|
|
debug_add('No usable uri found, skipping embed', MIDCOM_LOG_WARN); |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// replace src by embedded image |
158
|
|
|
$name = basename($uri); |
159
|
|
|
$mimetype = null; |
160
|
|
|
if ($ext = pathinfo($name, PATHINFO_EXTENSION)) { |
161
|
|
|
$mimetype = (new MimeTypes)->getMimeTypes($ext)[0] ?? null; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$part = (new DataPart(fopen($uri, 'r'), $name, $mimetype)) |
165
|
|
|
->asInline(); |
166
|
|
|
$this->_message->attachPart($part); |
167
|
|
|
|
168
|
|
|
$new_html = str_replace($location, 'cid:' . $part->getContentId(), $match); |
169
|
|
|
$this->_html_body = str_replace($match, $new_html, $this->_html_body); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
private function _process_attachments(array $attachments) |
174
|
|
|
{ |
175
|
2 |
|
foreach ($attachments as $att) { |
176
|
|
|
if (empty($att['mimetype'])) { |
177
|
|
|
$att['mimetype'] = "application/octet-stream"; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// we got a file path |
181
|
|
|
if (!empty($att['file'])) { |
182
|
|
|
$this->_message->attachFromPath($att['file'], $att['name'], $att['mimetype']); |
183
|
|
|
} |
184
|
|
|
// we got the contents (bytes) |
185
|
|
|
elseif (!empty($att['content'])) { |
186
|
|
|
$this->_message->attach($att['content'], $att['name'], $att['mimetype']); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|