-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtbphpmailer.php
349 lines (329 loc) · 14.1 KB
/
tbphpmailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Copyright (C) 2017-2024 thirty bees
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author E-Com <[email protected]>
* @author thirty bees <[email protected]>
* @copyright 2017-2024 thirty bees
* @license Academic Free License (AFL 3.0)
*/
if (!defined('_TB_VERSION_')) {
exit;
}
class TbPhpMailer extends Module
{
const SUBMIT = 'submitSettings';
const ENCRYPTION_NONE = 'none';
const ENCRYPTION_SSL = 'ssl';
const ENCRYPTION_TLS = 'tls';
const CONFIG_MAIL_SERVER = 'TBPHPMAILER_MAIL_SERVER';
const CONFIG_MAIL_USER = 'TBPHPMAILER_MAIL_USER';
const CONFIG_MAIL_PASSWD = 'TBPHPMAILER_MAIL_PASSWD';
const CONFIG_MAIL_SMTP_ENCRYPTION = 'TBPHPMAILER_MAIL_SMTP_ENCRYPTION';
const CONFIG_MAIL_SMTP_PORT = 'TBPHPMAILER_MAIL_SMTP_PORT';
const CONFIG_SMTP_AUTH = 'TBPHPMAILER_SMTP_AUTH';
const CONFIG_SMTP_AUTO_TLS = 'TBPHPMAILER_SMTP_AUTO_TLS';
const CONFIG_SSL_ALLOW_SELF_SIGN = 'TBPHPMAILER_SSL_ALLOW_SELF_SIGN';
const CONFIG_SSL_VERIFY_PEER = 'TBPHPMAILER_SSL_VERIFY_PEER' ;
const CONFIG_SSL_PEER_NAME = 'TBPHPMAILER_SSL_PEER_NAME';
const CONFIG_SSL_CA_FILE = 'TBPHPMAILER_SSL_CA_FILE';
const CONFIG_SSL_VERIFY_PEER_NAME = 'TBPHPMAILER_SSL_VERIFY_PEER_NAME';
/**
* @throws PrestaShopException
*/
public function __construct()
{
$this->name = 'tbphpmailer';
$this->tab = 'administration';
$this->version = '1.1.1';
$this->author = 'thirty bees';
$this->controllers = [];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Mail via PHPmailer');
$this->description = $this->l('This module implements mail functionality using PHPmailer library.');
$this->need_instance = 0;
$this->tb_versions_compliancy = '> 1.4.0';
$this->tb_min_version = '1.5.0';
}
/**
* @return bool
*
* @throws PrestaShopException
*/
public function install()
{
return (
parent::install() &&
$this->ensureConfiguration() &&
$this->registerHook('actionRegisterMailTransport')
);
}
/**
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function uninstall()
{
$this->removeConfiguration();
return parent::uninstall();
}
/**
* @return TbPhpMailerModule\PhpMailerTransport
*/
public function hookActionRegisterMailTransport()
{
require_once(__DIR__ . '/vendor/autoload.php');
return new TbPhpMailerModule\PhpMailerTransport();
}
/**
* @throws PrestaShopException
* @throws SmartyException
*/
public function getContent()
{
$this->ensureConfiguration();
$html = '';
if (Tools::isSubmit(static::SUBMIT)) {
$this->updateOptions();
$html .= $this->displayConfirmation($this->l('Configuration updated'));
}
$helper = new HelperOptions();
$helper->module = $this;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$helper->title = $this->displayName;
$helper->show_toolbar = false;
$hasPassword = !!Configuration::get(static::CONFIG_MAIL_PASSWD);
$settingsForm = [
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
'fields' => [
static::CONFIG_MAIL_SERVER => [
'title' => $this->l('SMTP server'),
'hint' => $this->l('IP address or server name (e.g. smtp.mydomain.com).'),
'validation' => 'isGenericName',
'type' => 'text',
'class' => 'fixed-width-xxl',
],
static::CONFIG_SMTP_AUTH => [
'title' => $this->l('Enable SMTP authentication'),
'hint' => $this->l('If enabled, username nad password will be used to authenticate user on smtp server'),
'cast' => 'boolval',
'type' => 'bool',
],
static::CONFIG_MAIL_USER => [
'title' => $this->l('SMTP username'),
'validation' => 'isGenericName',
'type' => 'text',
'class' => 'fixed-width-xxl',
],
static::CONFIG_MAIL_PASSWD => [
'title' => $this->l('SMTP password'),
'validation' => 'isAnything',
'type' => 'password',
'autocomplete' => false,
'class' => 'fixed-width-xxl',
'placeholder' => $hasPassword ? $this->l('Use saved password') : null,
'hint' => $hasPassword
? $this->l('Leave this field empty to keep using saved password')
: $this->l('Leave blank if not applicable.')
],
static::CONFIG_MAIL_SMTP_ENCRYPTION => [
'title' => $this->l('Encryption'),
'type' => 'select',
'cast' => 'strval',
'identifier' => 'mode',
'list' => [
[
'mode' => static::ENCRYPTION_SSL,
'name' => $this->l('SSL'),
],
[
'mode' => static::ENCRYPTION_TLS,
'name' => $this->l('TLS'),
],
[
'mode' => static::ENCRYPTION_NONE,
'name' => $this->l('None'),
],
],
],
static::CONFIG_MAIL_SMTP_PORT => [
'title' => $this->l('TCP port'),
'desc' => $this->l('The most commonly used ports: for SSL 465, for TLS 587, for none 25.'),
'validation' => 'isInt',
'type' => 'text',
'cast' => 'intval',
'class' => 'fixed-width-sm',
],
static::CONFIG_SMTP_AUTO_TLS => [
'title' => $this->l('Auto TLS'),
'hint' => $this->l('Attempt to automaticaly enable TLS encryption if server supports it'),
'cast' => 'boolval',
'type' => 'bool',
],
],
'submit' => [
'name' => static::SUBMIT,
'title' => $this->l('Save'),
'class' => 'button',
],
];
$sslForm = [
'title' => $this->l('SSL settings'),
'description' => $this->l('You can fine-tune SSL options that will be used for SSL or TLS encryption. The default settings works in most cases. Change these values only if you have issues with connecting to your SMTP server.'),
'icon' => 'icon-cogs',
'fields' => [
static::CONFIG_SSL_VERIFY_PEER => [
'title' => $this->l('Verify peer'),
'hint' => $this->l('Require verification of SSL certificate used.'),
'cast' => 'boolval',
'type' => 'bool',
],
static::CONFIG_SSL_VERIFY_PEER_NAME => [
'title' => $this->l('Verify peer name'),
'hint' => $this->l('Require verification of peer name.'),
'cast' => 'boolval',
'type' => 'bool',
],
static::CONFIG_SSL_ALLOW_SELF_SIGN => [
'title' => $this->l('Allow self-signed certificates'),
'hint' => $this->l('When enabled, mail server can use self-signed certificates. Requires "Verify peer"'),
'cast' => 'boolval',
'type' => 'bool',
],
static::CONFIG_SSL_PEER_NAME => [
'title' => $this->l('Peer name'),
'hint' => $this->l('Peer name used for certificate authentication. If not set, then the name is guessed based on the hostname'),
'type' => 'text',
'class' => 'fixed-width-xxl',
],
static::CONFIG_SSL_CA_FILE => [
'title' => $this->l('CA File Path'),
'hint' => $this->l('Location of Certificate Authority file on your server filesystem which should be used with the verify_peer context option to authenticate the identity of the remote peer'),
'type' => 'text',
],
],
'submit' => [
'name' => static::SUBMIT,
'title' => $this->l('Save'),
'class' => 'button',
],
];
return $html . $helper->generateOptions([
$settingsForm,
$sslForm,
]);
}
/**
* @throws PrestaShopException
*/
private function updateOptions()
{
// Update general settings options
Configuration::updateValue(static::CONFIG_MAIL_SERVER, Tools::getValue(static::CONFIG_MAIL_SERVER));
Configuration::updateValue(static::CONFIG_MAIL_USER, Tools::getValue(static::CONFIG_MAIL_USER));
Configuration::updateValue(static::CONFIG_MAIL_SMTP_ENCRYPTION, Tools::getValue(static::CONFIG_MAIL_SMTP_ENCRYPTION));
Configuration::updateValue(static::CONFIG_MAIL_SMTP_PORT, (int)Tools::getValue(static::CONFIG_MAIL_SMTP_PORT));
Configuration::updateValue(static::CONFIG_SMTP_AUTO_TLS, (int)Tools::getValue(static::CONFIG_SMTP_AUTO_TLS));
Configuration::updateValue(static::CONFIG_SMTP_AUTH, (int)Tools::getValue(static::CONFIG_SMTP_AUTH));
// update password only when set
$password = Tools::getValue(static::CONFIG_MAIL_PASSWD);
if ($password !== '' && $password !== false) {
Configuration::updateValue(static::CONFIG_MAIL_PASSWD, $password);
}
// Update SSL options
Configuration::updateValue(static::CONFIG_SSL_PEER_NAME, Tools::getValue(static::CONFIG_SSL_PEER_NAME));
Configuration::updateValue(static::CONFIG_SSL_CA_FILE, Tools::getValue(static::CONFIG_SSL_CA_FILE));
Configuration::updateValue(static::CONFIG_SSL_ALLOW_SELF_SIGN, (int)Tools::getValue(static::CONFIG_SSL_ALLOW_SELF_SIGN));
Configuration::updateValue(static::CONFIG_SSL_VERIFY_PEER, (int)Tools::getValue(static::CONFIG_SSL_VERIFY_PEER));
Configuration::updateValue(static::CONFIG_SSL_VERIFY_PEER_NAME, (int)Tools::getValue(static::CONFIG_SSL_VERIFY_PEER_NAME));
}
/**
* @return bool
*
* @throws PrestaShopException
*/
private function ensureConfiguration()
{
$this->ensureConfigKey(static::CONFIG_SMTP_AUTH, '1');
$this->ensureConfigKey(static::CONFIG_SMTP_AUTO_TLS, '1');
$this->ensureConfigKey(static::CONFIG_MAIL_SERVER, (string)Configuration::get('PS_MAIL_SERVER'));
$this->ensureConfigKey(static::CONFIG_MAIL_USER, (string)Configuration::get('PS_MAIL_USER'));
$this->ensureConfigKey(static::CONFIG_MAIL_PASSWD, (string)Configuration::get('PS_MAIL_PASSWD'));
$this->ensureConfigKey(static::CONFIG_MAIL_SMTP_ENCRYPTION, static::getEncryptionValue((string)Configuration::get('PS_MAIL_SMTP_ENCRYPTION')));
$port = (int)Configuration::get('PS_MAIL_SMTP_PORT');
if (! $port) {
$port = 25;
}
$this->ensureConfigKey(static::CONFIG_MAIL_SMTP_PORT, (string)$port);
$this->ensureConfigKey(static::CONFIG_SSL_ALLOW_SELF_SIGN, '0');
$this->ensureConfigKey(static::CONFIG_SSL_VERIFY_PEER, '1');
$this->ensureConfigKey(static::CONFIG_SSL_PEER_NAME, '');
$this->ensureConfigKey(static::CONFIG_SSL_CA_FILE, '');
$this->ensureConfigKey(static::CONFIG_SSL_VERIFY_PEER_NAME, '1');
return true;
}
/**
* @param string $key
* @param string $defaultValue
*
* @return void
*
* @throws PrestaShopException
*/
private function ensureConfigKey(string $key, string $defaultValue)
{
if (! Configuration::hasKey($key)) {
Configuration::updateValue($key, $defaultValue);
}
}
/**
* @return void
*
* @throws PrestaShopException
*/
private function removeConfiguration()
{
Configuration::deleteByName(static::CONFIG_MAIL_SERVER);
Configuration::deleteByName(static::CONFIG_MAIL_USER);
Configuration::deleteByName(static::CONFIG_MAIL_PASSWD);
Configuration::deleteByName(static::CONFIG_MAIL_SMTP_ENCRYPTION);
Configuration::deleteByName(static::CONFIG_MAIL_SMTP_PORT);
Configuration::deleteByName(static::CONFIG_SSL_ALLOW_SELF_SIGN);
Configuration::deleteByName(static::CONFIG_SSL_VERIFY_PEER);
Configuration::deleteByName(static::CONFIG_SSL_PEER_NAME);
Configuration::deleteByName(static::CONFIG_SSL_CA_FILE);
Configuration::deleteByName(static::CONFIG_SSL_VERIFY_PEER_NAME);
Configuration::deleteByName(static::CONFIG_SMTP_AUTO_TLS);
Configuration::deleteByName(static::CONFIG_SMTP_AUTH);
}
/**
* @param string $key
* @return string
*/
public static function getEncryptionValue(string $key): string
{
switch (strtolower($key)) {
case static::ENCRYPTION_SSL:
return static::ENCRYPTION_SSL;
case static::ENCRYPTION_TLS:
return static::ENCRYPTION_TLS;
case static::ENCRYPTION_NONE:
default:
return static::ENCRYPTION_NONE;
}
}
}