1: | <?php
|
2: | namespace Opencart\System\Library\Mail;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Mail {
|
9: | |
10: | |
11: |
|
12: | protected array $option = [];
|
13: |
|
14: | |
15: | |
16: | |
17: | |
18: |
|
19: | public function __construct(array &$option = []) {
|
20: | $this->option = &$option;
|
21: | }
|
22: |
|
23: | |
24: | |
25: | |
26: | |
27: |
|
28: | public function send(): bool {
|
29: | if (is_array($this->option['to'])) {
|
30: | $to = implode(',', $this->option['to']);
|
31: | } else {
|
32: | $to = $this->option['to'];
|
33: | }
|
34: |
|
35: | if (version_compare(PHP_VERSION, '8.0', '>=')) {
|
36: | $eol = "\r\n";
|
37: | } else {
|
38: | $eol = PHP_EOL;
|
39: | }
|
40: |
|
41: | $boundary = '----=_NextPart_' . md5((string)time());
|
42: |
|
43: | $header = 'MIME-Version: 1.0' . $eol;
|
44: | $header .= 'Date: ' . date('D, d M Y H:i:s O') . $eol;
|
45: | $header .= 'From: =?UTF-8?B?' . base64_encode($this->option['sender']) . '?= <' . $this->option['from'] . '>' . $eol;
|
46: |
|
47: | if (empty($this->option['reply_to'])) {
|
48: | $header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->option['sender']) . '?= <' . $this->option['from'] . '>' . $eol;
|
49: | } else {
|
50: | $header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->option['reply_to']) . '?= <' . $this->option['reply_to'] . '>' . $eol;
|
51: | }
|
52: |
|
53: | $header .= 'Return-Path: ' . $this->option['from'] . $eol;
|
54: | $header .= 'X-Mailer: PHP/' . PHP_VERSION . $eol;
|
55: | $header .= 'Content-Type: multipart/mixed; boundary="' . $boundary . '"' . $eol . $eol;
|
56: |
|
57: | $message = '--' . $boundary . $eol;
|
58: |
|
59: | if (empty($this->option['html'])) {
|
60: | $message .= 'Content-Type: text/plain; charset="utf-8"' . $eol;
|
61: | $message .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
|
62: | $message .= chunk_split(base64_encode($this->option['text']), 950) . $eol;
|
63: | } else {
|
64: | $message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $eol . $eol;
|
65: | $message .= '--' . $boundary . '_alt' . $eol;
|
66: | $message .= 'Content-Type: text/plain; charset="utf-8"' . $eol;
|
67: | $message .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
|
68: |
|
69: | if (!empty($this->option['text'])) {
|
70: | $message .= chunk_split(base64_encode($this->option['text']), 950) . $eol;
|
71: | } else {
|
72: | $message .= chunk_split(base64_encode('This is a HTML email and your email client software does not support HTML email!'), 950) . $eol;
|
73: | }
|
74: |
|
75: | $message .= '--' . $boundary . '_alt' . $eol;
|
76: | $message .= 'Content-Type: text/html; charset="utf-8"' . $eol;
|
77: | $message .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
|
78: | $message .= chunk_split(base64_encode($this->option['html']), 950) . $eol;
|
79: | $message .= '--' . $boundary . '_alt--' . $eol;
|
80: | }
|
81: |
|
82: | if (!empty($this->option['attachments'])) {
|
83: | foreach ($this->option['attachments'] as $attachment) {
|
84: | if (is_file($attachment)) {
|
85: | $handle = fopen($attachment, 'r');
|
86: |
|
87: | $content = fread($handle, filesize($attachment));
|
88: |
|
89: | fclose($handle);
|
90: |
|
91: | $message .= '--' . $boundary . $eol;
|
92: | $message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $eol;
|
93: | $message .= 'Content-Transfer-Encoding: base64' . $eol;
|
94: | $message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $eol;
|
95: | $message .= 'Content-ID: <' . urlencode(basename($attachment)) . '>' . $eol;
|
96: | $message .= 'X-Attachment-Id: ' . urlencode(basename($attachment)) . $eol . $eol;
|
97: | $message .= chunk_split(base64_encode($content), 950);
|
98: | }
|
99: | }
|
100: | }
|
101: |
|
102: | $message .= '--' . $boundary . '--' . $eol;
|
103: |
|
104: | ini_set('sendmail_from', $this->option['from']);
|
105: |
|
106: | if (!empty($this->option['parameter'])) {
|
107: | return mail($to, '=?UTF-8?B?' . base64_encode($this->option['subject']) . '?=', $message, $header, $this->option['parameter']);
|
108: | } else {
|
109: | return mail($to, '=?UTF-8?B?' . base64_encode($this->option['subject']) . '?=', $message, $header);
|
110: | }
|
111: | }
|
112: | }
|
113: | |