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