1: <?php
2: /**
3: * @package OpenCart
4: *
5: * @author Daniel Kerr
6: * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
7: * @license https://opensource.org/licenses/GPL-3.0
8: *
9: * @see https://www.opencart.com
10: */
11: namespace Opencart\System\Library;
12: /**
13: * Class Mail
14: */
15: class Mail {
16: private object $adaptor;
17: /**
18: * @var array<string, mixed>
19: */
20: private array $option = [];
21:
22: /**
23: * Constructor
24: *
25: * @param string $adaptor
26: * @param array<string, mixed> $option
27: */
28: public function __construct(string $adaptor = 'mail', array $option = []) {
29: $class = 'Opencart\System\Library\Mail\\' . $adaptor;
30:
31: if (class_exists($class)) {
32: $this->option = &$option;
33:
34: $this->adaptor = new $class($option);
35: } else {
36: throw new \Exception('Error: Could not load mail adaptor ' . $adaptor . '!');
37: }
38: }
39:
40: /**
41: * setTo
42: *
43: * @param array<string>|string $to
44: *
45: * @return void
46: */
47: public function setTo($to): void {
48: $this->option['to'] = $to;
49: }
50:
51: /**
52: * setFrom
53: *
54: * @param string $from
55: *
56: * @return void
57: */
58: public function setFrom(string $from): void {
59: $this->option['from'] = $from;
60: }
61:
62: /**
63: * setSender
64: *
65: * @param string $sender
66: *
67: * @return void
68: */
69: public function setSender(string $sender): void {
70: $this->option['sender'] = $sender;
71: }
72:
73: /**
74: * setReplyTo
75: *
76: * @param string $reply_to
77: *
78: * @return void
79: */
80: public function setReplyTo(string $reply_to): void {
81: $this->option['reply_to'] = $reply_to;
82: }
83:
84: /**
85: * setSubject
86: *
87: * @param string $subject
88: *
89: * @return void
90: */
91: public function setSubject(string $subject): void {
92: $this->option['subject'] = $subject;
93: }
94:
95: /**
96: * setText
97: *
98: * @param string $text
99: *
100: * @return void
101: */
102: public function setText(string $text): void {
103: $this->option['text'] = $text;
104: }
105:
106: /**
107: * setHtml
108: *
109: * @param string $html
110: *
111: * @return void
112: */
113: public function setHtml(string $html): void {
114: $this->option['html'] = $html;
115: }
116:
117: /**
118: * addAttachment
119: *
120: * @param string $filename
121: *
122: * @return void
123: */
124: public function addAttachment(string $filename): void {
125: $this->option['attachments'][] = $filename;
126: }
127:
128: /**
129: * Send
130: *
131: * @return bool
132: */
133: public function send(): bool {
134: if (empty($this->option['to'])) {
135: throw new \Exception('Error: E-Mail to required!');
136: }
137:
138: if (empty($this->option['from'])) {
139: throw new \Exception('Error: E-Mail from required!');
140: }
141:
142: if (empty($this->option['sender'])) {
143: throw new \Exception('Error: E-Mail sender required!');
144: }
145:
146: if (empty($this->option['subject'])) {
147: throw new \Exception('Error: E-Mail subject required!');
148: }
149:
150: if (empty($this->option['text']) && empty($this->option['html'])) {
151: throw new \Exception('Error: E-Mail message required!');
152: }
153:
154: return $this->adaptor->send();
155: }
156: }
157: