1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Mail |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\mail; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Library, |
13
|
|
|
gplcart\core\Module as CoreModule; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Main class for Mail module |
17
|
|
|
*/ |
18
|
|
|
class Module |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Module class instance |
23
|
|
|
* @var \gplcart\core\Module $module |
24
|
|
|
*/ |
25
|
|
|
protected $module; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Library class instance |
29
|
|
|
* @var \gplcart\core\Library $library |
30
|
|
|
*/ |
31
|
|
|
protected $library; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param CoreModule $module |
35
|
|
|
* @param Library $library |
36
|
|
|
*/ |
37
|
|
|
public function __construct(CoreModule $module, Library $library) |
38
|
|
|
{ |
39
|
|
|
$this->module = $module; |
40
|
|
|
$this->library = $library; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Implements hook "library.list" |
45
|
|
|
* @param array $libraries |
46
|
|
|
*/ |
47
|
|
|
public function hookLibraryList(array &$libraries) |
48
|
|
|
{ |
49
|
|
|
$libraries['phpmailer'] = array( |
50
|
|
|
'name' => /* @text */'PHP Mail', |
51
|
|
|
'description' => /* @text */'The classic email sending library for PHP', |
52
|
|
|
'url' => 'https://github.com/PHPMailer/PHPMailer', |
53
|
|
|
'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip', |
54
|
|
|
'type' => 'php', |
55
|
|
|
'version_source' => array( |
56
|
|
|
'lines' => 100, |
57
|
|
|
'pattern' => '/.*\\$Version.*(\\d+\\.+\\d+\\.+\\d+)/', |
58
|
|
|
'file' => 'vendor/phpmailer/phpmailer/class.phpmailer.php' |
59
|
|
|
), |
60
|
|
|
'module' => 'mail', |
61
|
|
|
'files' => array( |
62
|
|
|
'vendor/autoload.php' |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Implements hook "route.list" |
69
|
|
|
* @param array $routes |
70
|
|
|
*/ |
71
|
|
|
public function hookRouteList(array &$routes) |
72
|
|
|
{ |
73
|
|
|
$routes['admin/module/settings/mail'] = array( |
74
|
|
|
'access' => 'module_edit', |
75
|
|
|
'handlers' => array( |
76
|
|
|
'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings') |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Implements hook "mail.send" |
83
|
|
|
* @param array $to |
84
|
|
|
* @param string $subject |
85
|
|
|
* @param string $message |
86
|
|
|
* @param array $options |
87
|
|
|
* @param mixed $result |
88
|
|
|
*/ |
89
|
|
|
public function hookMailSend($to, $subject, $message, $options, &$result) |
90
|
|
|
{ |
91
|
|
|
$this->setMailer($to, $subject, $message, $options, $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Implements hook "module.enable.after" |
96
|
|
|
*/ |
97
|
|
|
public function hookModuleEnableAfter() |
98
|
|
|
{ |
99
|
|
|
$this->library->clearCache(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Implements hook "module.disable.after" |
104
|
|
|
*/ |
105
|
|
|
public function hookModuleDisableAfter() |
106
|
|
|
{ |
107
|
|
|
$this->library->clearCache(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Implements hook "module.install.after" |
112
|
|
|
*/ |
113
|
|
|
public function hookModuleInstallAfter() |
114
|
|
|
{ |
115
|
|
|
$this->library->clearCache(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Implements hook "module.uninstall.after" |
120
|
|
|
*/ |
121
|
|
|
public function hookModuleUninstallAfter() |
122
|
|
|
{ |
123
|
|
|
$this->library->clearCache(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Send an E-mail |
128
|
|
|
* @param array $to |
129
|
|
|
* @param string $subject |
130
|
|
|
* @param string $message |
131
|
|
|
* @param array $options |
132
|
|
|
* @param array $settings |
133
|
|
|
* @return boolean|string |
134
|
|
|
*/ |
135
|
|
|
public function send($to, $subject, $message, $options, $settings) |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
$mailer = $this->getMailer(); |
139
|
|
|
} catch (\InvalidArgumentException $ex) { |
140
|
|
|
return $ex->getMessage(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$mailer->isSMTP(); |
144
|
|
|
$mailer->isHTML(!empty($options['html'])); |
145
|
|
|
|
146
|
|
|
$mailer->Body = $message; |
147
|
|
|
$mailer->Subject = $subject; |
148
|
|
|
|
149
|
|
|
if (!empty($options['html'])) { |
150
|
|
|
$mailer->AltBody = strip_tags($message); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$mailer->Port = $settings['port']; |
154
|
|
|
$mailer->setFrom($options['from']); |
155
|
|
|
$mailer->Username = $settings['user']; |
156
|
|
|
$mailer->Password = $settings['password']; |
157
|
|
|
$mailer->SMTPSecure = $settings['secure']; |
158
|
|
|
$mailer->SMTPAuth = !empty($settings['auth']); |
159
|
|
|
$mailer->Host = implode(';', $settings['host']); |
160
|
|
|
|
161
|
|
|
foreach ($to as $address) { |
162
|
|
|
settype($address, 'array'); |
163
|
|
|
call_user_func_array(array($mailer, 'addAddress'), $address); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!empty($options['attachment'])) { |
167
|
|
|
foreach ($options['attachment'] as $attachment) { |
168
|
|
|
settype($attachment, 'array'); |
169
|
|
|
call_user_func_array(array($mailer, 'addAttachment'), $attachment); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($mailer->send()) { |
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return (string) $mailer->ErrorInfo; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns PHPMailer instance |
182
|
|
|
* @return \PHPMailer |
183
|
|
|
* @throws \InvalidArgumentException |
184
|
|
|
*/ |
185
|
|
|
public function getMailer() |
186
|
|
|
{ |
187
|
|
|
$this->library->load('phpmailer'); |
188
|
|
|
|
189
|
|
|
if (class_exists('PHPMailer')) { |
190
|
|
|
return new \PHPMailer; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
throw new \InvalidArgumentException('Class PHPMailer not found'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $to |
198
|
|
|
* @param string $subject |
199
|
|
|
* @param string $message |
200
|
|
|
* @param array $options |
201
|
|
|
* @param mixed $result |
202
|
|
|
*/ |
203
|
|
|
protected function setMailer($to, $subject, $message, $options, &$result) |
204
|
|
|
{ |
205
|
|
|
$settings = $this->module->getSettings('mail'); |
206
|
|
|
|
207
|
|
|
if (!empty($settings['status']) && $result === null) { |
208
|
|
|
$sent = $this->send($to, $subject, $message, $options, $settings); |
209
|
|
|
if ($sent === true) { |
210
|
|
|
$result = true; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|