|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* osCommerce Online Merchant |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (c) 2016 osCommerce; https://www.oscommerce.com |
|
6
|
|
|
* @license GPL; https://www.oscommerce.com/gpllicense.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OSC\OM; |
|
10
|
|
|
|
|
11
|
|
|
class Mail |
|
12
|
|
|
{ |
|
13
|
|
|
protected $to = [], |
|
14
|
|
|
$from = [], |
|
15
|
|
|
$cc = [], |
|
16
|
|
|
$bcc = [], |
|
17
|
|
|
$subject, |
|
18
|
|
|
$body_plain, |
|
19
|
|
|
$body_html, |
|
20
|
|
|
$attachments = [], |
|
21
|
|
|
$images = [], |
|
22
|
|
|
$boundary, |
|
23
|
|
|
$headers = ['X-Mailer' => 'osCommerce'], |
|
24
|
|
|
$body, |
|
25
|
|
|
$content_transfer_encoding = '7bit', |
|
26
|
|
|
$charset = 'utf-8'; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($to_email_address = null, $to = null, $from_email_address = null, $from = null, $subject = null) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!empty($to_email_address)) { |
|
31
|
|
|
$this->addTo($to_email_address, $to); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!empty($from_email_address)) { |
|
35
|
|
|
$this->setFrom($from_email_address, $from); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!empty($subject)) { |
|
39
|
|
|
$this->setSubject($subject); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function addTo($email_address, $name = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->to[] = [ |
|
46
|
|
|
'name' => $name, |
|
47
|
|
|
'email_address' => $email_address |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function setFrom($email_address, $name = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->from = [ |
|
54
|
|
|
'name' => $name, |
|
55
|
|
|
'email_address' => $email_address |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function addCC($email_address, $name = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->cc[] = [ |
|
62
|
|
|
'name' => $name, |
|
63
|
|
|
'email_address' => $email_address |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function addBCC($email_address, $name = null) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->bcc[] = [ |
|
70
|
|
|
'name' => $name, |
|
71
|
|
|
'email_address' => $email_address |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function clearTo() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->to = []; |
|
78
|
|
|
$this->cc = []; |
|
79
|
|
|
$this->bcc = []; |
|
80
|
|
|
$this->headers = [ |
|
81
|
|
|
'X-Mailer' => 'osCommerce' |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setSubject($subject) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->subject = $subject; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setBody($html) |
|
91
|
|
|
{ |
|
92
|
|
|
$plain = strip_tags($html); |
|
93
|
|
|
|
|
94
|
|
|
$this->setBodyHTML($html); |
|
95
|
|
|
$this->setBodyPlain($plain); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function setBodyPlain($body) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->body_plain = $body; |
|
101
|
|
|
$this->body = null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function setBodyHTML($body) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->body_html = $body; |
|
107
|
|
|
$this->body = null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setContentTransferEncoding($encoding) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->content_transfer_encoding = $encoding; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function setCharset($charset) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->charset = $charset; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function addHeader($key, $value) |
|
121
|
|
|
{ |
|
122
|
|
|
if ((strpos($key, "\n") !== false) || (strpos($key, "\r") !== false)) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ((strpos($value, "\n") !== false) || (strpos($value, "\r") !== false)) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->headers[$key] = $value; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function addAttachment($file, $is_uploaded = false) |
|
134
|
|
|
{ |
|
135
|
|
View Code Duplication |
if ($is_uploaded === true) { |
|
|
|
|
|
|
136
|
|
|
} elseif (file_exists($file) && is_readable($file)) { |
|
137
|
|
|
$data = file_get_contents($file); |
|
138
|
|
|
$filename = basename($file); |
|
139
|
|
|
$mimetype = $this->get_mime_type($filename); |
|
140
|
|
|
} else { |
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->attachments[] = [ |
|
145
|
|
|
'filename' => $filename, |
|
|
|
|
|
|
146
|
|
|
'mimetype' => $mimetype, |
|
|
|
|
|
|
147
|
|
|
'data' => chunk_split(base64_encode($data)) |
|
|
|
|
|
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function addImage($file, $is_uploaded = false) |
|
152
|
|
|
{ |
|
153
|
|
View Code Duplication |
if ($is_uploaded === true) { |
|
|
|
|
|
|
154
|
|
|
} elseif (file_exists($file) && is_readable($file)) { |
|
155
|
|
|
$data = file_get_contents($file); |
|
156
|
|
|
$filename = basename($file); |
|
157
|
|
|
$mimetype = $this->get_mime_type($filename); |
|
158
|
|
|
} else { |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->images[] = [ |
|
163
|
|
|
'id' => md5(uniqid(time())), |
|
164
|
|
|
'filename' => $filename, |
|
|
|
|
|
|
165
|
|
|
'mimetype' => $mimetype, |
|
|
|
|
|
|
166
|
|
|
'data' => chunk_split(base64_encode($data)) |
|
|
|
|
|
|
167
|
|
|
]; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function send() |
|
171
|
|
|
{ |
|
172
|
|
|
if (empty($this->body)) { |
|
173
|
|
|
if (!empty($this->body_plain) && !empty($this->body_html)) { |
|
174
|
|
|
$this->boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
|
175
|
|
|
$this->related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
|
|
|
|
|
|
176
|
|
|
$this->alternative_boundary = '=_____MULTIPART_ALTERNATIVE_BOUNDARY____'; |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$this->headers['MIME-Version'] = '1.0'; |
|
179
|
|
|
$this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->boundary . '"'; |
|
180
|
|
|
$this->headers['Content-Transfer-Encoding'] = $this->content_transfer_encoding; |
|
181
|
|
|
|
|
182
|
|
|
if (!empty($this->images)) { |
|
183
|
|
View Code Duplication |
foreach ($this->images as $image) { |
|
|
|
|
|
|
184
|
|
|
$this->body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->body_html); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
unset($image); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
|
191
|
|
|
'--' . $this->boundary . "\n"; |
|
192
|
|
|
|
|
193
|
|
|
$this->body .= 'Content-Type: multipart/alternative; boundary="' . $this->alternative_boundary . '";' . "\n\n" . |
|
|
|
|
|
|
194
|
|
|
'--' . $this->alternative_boundary . "\n" . |
|
|
|
|
|
|
195
|
|
|
'Content-Type: text/plain; charset="' . $this->charset . '"' . "\n" . |
|
196
|
|
|
'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
|
197
|
|
|
$this->body_plain . "\n\n" . |
|
198
|
|
|
'--' . $this->alternative_boundary . "\n" . |
|
|
|
|
|
|
199
|
|
|
'Content-Type: multipart/related; boundary="' . $this->related_boundary . '"' . "\n\n" . |
|
|
|
|
|
|
200
|
|
|
'--' . $this->related_boundary . "\n" . |
|
|
|
|
|
|
201
|
|
|
'Content-Type: text/html; charset="' . $this->charset . '"' . "\n" . |
|
202
|
|
|
'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
|
203
|
|
|
$this->body_html . "\n\n"; |
|
204
|
|
|
|
|
205
|
|
|
if (!empty($this->images)) { |
|
206
|
|
|
foreach ($this->images as $image) { |
|
207
|
|
|
$this->body .= $this->build_image($image, $this->related_boundary); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
unset($image); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->body .= '--' . $this->related_boundary . '--' . "\n\n" . |
|
|
|
|
|
|
214
|
|
|
'--' . $this->alternative_boundary . '--' . "\n\n"; |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
if (!empty($this->attachments)) { |
|
217
|
|
|
foreach ($this->attachments as $attachment) { |
|
218
|
|
|
$this->body .= $this->build_attachment($attachment, $this->boundary); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
unset($attachment); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$this->body .= '--' . $this->boundary . '--' . "\n\n"; |
|
225
|
|
|
} elseif (!empty($this->body_html) && !empty($this->images)) { |
|
226
|
|
|
$this->boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
|
227
|
|
|
$this->related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$this->headers['MIME-Version'] = '1.0'; |
|
230
|
|
|
$this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->boundary . '"'; |
|
231
|
|
|
|
|
232
|
|
View Code Duplication |
foreach ($this->images as $image) { |
|
|
|
|
|
|
233
|
|
|
$this->body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->body_html); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
unset($image); |
|
237
|
|
|
|
|
238
|
|
|
$this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
|
239
|
|
|
'--' . $this->boundary . "\n" . |
|
240
|
|
|
'Content-Type: multipart/related; boundary="' . $this->related_boundary . '";' . "\n\n" . |
|
|
|
|
|
|
241
|
|
|
'--' . $this->related_boundary . "\n" . |
|
|
|
|
|
|
242
|
|
|
'Content-Type: text/html; charset="' . $this->charset . '"' . "\n" . |
|
243
|
|
|
'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
|
244
|
|
|
$this->body_html . "\n\n"; |
|
245
|
|
|
|
|
246
|
|
|
foreach ($this->images as $image) { |
|
247
|
|
|
$this->body .= $this->build_image($image, $this->related_boundary); |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
unset($image); |
|
251
|
|
|
|
|
252
|
|
|
$this->body .= '--' . $this->related_boundary . '--' . "\n\n"; |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
foreach ($this->attachments as $attachment) { |
|
255
|
|
|
$this->body .= $this->build_attachment($attachment, $this->boundary); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
unset($attachment); |
|
259
|
|
|
|
|
260
|
|
|
$this->body .= '--' . $this->boundary . '--' . "\n"; |
|
261
|
|
|
} elseif (!empty($this->attachments)) { |
|
262
|
|
|
$this->boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
|
263
|
|
|
$this->related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
|
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
$this->headers['MIME-Version'] = '1.0'; |
|
266
|
|
|
$this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->boundary . '"'; |
|
267
|
|
|
|
|
268
|
|
|
$this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
|
269
|
|
|
'--' . $this->boundary . "\n" . |
|
270
|
|
|
'Content-Type: multipart/related; boundary="' . $this->related_boundary . '";' . "\n\n" . |
|
|
|
|
|
|
271
|
|
|
'--' . $this->related_boundary . "\n" . |
|
|
|
|
|
|
272
|
|
|
'Content-Type: text/' . (empty($this->body_plain) ? 'html' : 'plain') . '; charset="' . $this->charset . '"' . "\n" . |
|
273
|
|
|
'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
|
274
|
|
|
(empty($this->body_plain) ? $this->body_html : $this->body_plain) . "\n\n" . |
|
275
|
|
|
'--' . $this->related_boundary . '--' . "\n\n"; |
|
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
foreach ($this->attachments as $attachment) { |
|
278
|
|
|
$this->body .= $this->build_attachment($attachment, $this->boundary); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
unset($attachment); |
|
282
|
|
|
|
|
283
|
|
|
$this->body .= '--' . $this->boundary . '--' . "\n"; |
|
284
|
|
|
} elseif (!empty($this->body_html)) { |
|
285
|
|
|
$this->headers['MIME-Version'] = '1.0'; |
|
286
|
|
|
$this->headers['Content-Type'] = 'text/html; charset="' . $this->charset . '"'; |
|
287
|
|
|
$this->headers['Content-Transfer-Encoding'] = $this->content_transfer_encoding; |
|
288
|
|
|
|
|
289
|
|
|
$this->body = $this->body_html . "\n"; |
|
290
|
|
|
} else { |
|
291
|
|
|
$this->body = $this->body_plain . "\n"; |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
$to_email_addresses = []; |
|
296
|
|
|
|
|
297
|
|
|
foreach ($this->to as $to) { |
|
298
|
|
View Code Duplication |
if ((strpos($to['email_address'], "\n") !== false) || (strpos($to['email_address'], "\r") !== false)) { |
|
|
|
|
|
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
View Code Duplication |
if ((strpos($to['name'], "\n") !== false) || (strpos($to['name'], "\r") !== false)) { |
|
|
|
|
|
|
303
|
|
|
return false; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
if (empty($to['name'])) { |
|
307
|
|
|
$to_email_addresses[] = $to['email_address']; |
|
308
|
|
|
} else { |
|
309
|
|
|
$to_email_addresses[] = '"' . $to['name'] . '" <' . $to['email_address'] . '>'; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
unset($to); |
|
314
|
|
|
|
|
315
|
|
|
$cc_email_addresses = []; |
|
316
|
|
|
|
|
317
|
|
View Code Duplication |
foreach ($this->cc as $cc) { |
|
|
|
|
|
|
318
|
|
|
if (empty($cc['name'])) { |
|
319
|
|
|
$cc_email_addresses[] = $cc['email_address']; |
|
320
|
|
|
} else { |
|
321
|
|
|
$cc_email_addresses[] = '"' . $cc['name'] . '" <' . $cc['email_address'] . '>'; |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
unset($cc); |
|
326
|
|
|
|
|
327
|
|
|
$bcc_email_addresses = []; |
|
328
|
|
|
|
|
329
|
|
View Code Duplication |
foreach ($this->bcc as $bcc) { |
|
|
|
|
|
|
330
|
|
|
if (empty($bcc['name'])) { |
|
331
|
|
|
$bcc_email_addresses[] = $bcc['email_address']; |
|
332
|
|
|
} else { |
|
333
|
|
|
$bcc_email_addresses[] = '"' . $bcc['name'] . '" <' . $bcc['email_address'] . '>'; |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
unset($bcc); |
|
338
|
|
|
|
|
339
|
|
|
if (empty($this->from['name'])) { |
|
340
|
|
|
$this->addHeader('From', $this->from['email_address']); |
|
341
|
|
|
} else { |
|
342
|
|
|
$this->addHeader('From', '"' . $this->from['name'] . '" <' . $this->from['email_address'] . '>'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
if (!empty($cc_email_addresses)) { |
|
346
|
|
|
$this->addHeader('Cc', implode(', ', $cc_email_addresses)); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if (!empty($bcc_email_addresses)) { |
|
350
|
|
|
$this->addHeader('Bcc', implode(', ', $bcc_email_addresses)); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
$headers = ''; |
|
354
|
|
|
|
|
355
|
|
|
foreach ($this->headers as $key => $value) { |
|
356
|
|
|
$headers .= $key . ': ' . $value . "\n"; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
if (empty($this->from['email_address']) || empty($to_email_addresses)) { |
|
360
|
|
|
return false; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
if (empty($this->from['name'])) { |
|
364
|
|
|
ini_set('sendmail_from', $this->from['email_address']); |
|
365
|
|
|
} else { |
|
366
|
|
|
ini_set('sendmail_from', '"' . $this->from['name'] . '" <' . $this->from['email_address'] . '>'); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
mail(implode(', ', $to_email_addresses), $this->subject, $this->body, $headers, '-f' . $this->from['email_address']); |
|
370
|
|
|
|
|
371
|
|
|
ini_restore('sendmail_from'); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
protected function get_mime_type($file) |
|
375
|
|
|
{ |
|
376
|
|
|
$ext = substr($file, strrpos($file, '.') + 1); |
|
377
|
|
|
|
|
378
|
|
|
$mime_types = [ |
|
379
|
|
|
'gif' => 'image/gif', |
|
380
|
|
|
'jpg' => 'image/jpeg', |
|
381
|
|
|
'jpeg' => 'image/jpeg', |
|
382
|
|
|
'jpe' => 'image/jpeg', |
|
383
|
|
|
'bmp' => 'image/bmp', |
|
384
|
|
|
'png' => 'image/png', |
|
385
|
|
|
'tif' => 'image/tiff', |
|
386
|
|
|
'tiff' => 'image/tiff', |
|
387
|
|
|
'swf' => 'application/x-shockwave-flash' |
|
388
|
|
|
]; |
|
389
|
|
|
|
|
390
|
|
|
if (isset($mime_types[$ext])) { |
|
391
|
|
|
return $mime_types[$ext]; |
|
392
|
|
|
} else { |
|
393
|
|
|
return 'application/octet-stream'; |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
protected function build_attachment($attachment, $boundary) |
|
398
|
|
|
{ |
|
399
|
|
|
return '--' . $boundary . "\n" . |
|
400
|
|
|
'Content-Type: ' . $attachment['mimetype'] . '; name="' . $attachment['filename'] . '"' . "\n" . |
|
401
|
|
|
'Content-Disposition: attachment' . "\n" . |
|
402
|
|
|
'Content-Transfer-Encoding: base64' . "\n\n" . |
|
403
|
|
|
$attachment['data'] . "\n\n"; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
protected function build_image($image, $boundary) |
|
407
|
|
|
{ |
|
408
|
|
|
return '--' . $boundary . "\n" . |
|
409
|
|
|
'Content-Type: ' . $image['mimetype'] . '; name="' . $image['filename'] . '"' . "\n" . |
|
410
|
|
|
'Content-ID: ' . $image['id'] . "\n" . |
|
411
|
|
|
'Content-Disposition: inline' . "\n" . |
|
412
|
|
|
'Content-Transfer-Encoding: base64' . "\n\n" . |
|
413
|
|
|
$image['data'] . "\n\n"; |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.