-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.php
180 lines (155 loc) · 4.53 KB
/
Main.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
<?php
/**
* @package Mail
* @author Iurii Makukh
* @copyright Copyright (c) 2017, Iurii Makukh
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
*/
namespace gplcart\modules\mail;
use Exception;
use gplcart\core\Library;
use gplcart\core\Module;
use LogicException;
use PHPMailer;
/**
* Main class for Mail module
*/
class Main
{
/**
* Module class instance
* @var \gplcart\core\Module $module
*/
protected $module;
/**
* Library class instance
* @var \gplcart\core\Library $library
*/
protected $library;
/**
* @param Module $module
* @param Library $library
*/
public function __construct(Module $module, Library $library)
{
$this->module = $module;
$this->library = $library;
}
/**
* Implements hook "library.list"
* @param array $libraries
*/
public function hookLibraryList(array &$libraries)
{
$libraries['phpmailer'] = array(
'name' => 'PHP Mail', // @text
'description' => 'The classic email sending library for PHP', // @text
'url' => 'https://github.com/PHPMailer/PHPMailer',
'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip',
'type' => 'php',
'vendor' => 'phpmailer/phpmailer',
'version' => '5.2.23',
'module' => 'mail'
);
}
/**
* Implements hook "route.list"
* @param array $routes
*/
public function hookRouteList(array &$routes)
{
$routes['admin/module/settings/mail'] = array(
'access' => 'module_edit',
'handlers' => array(
'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings')
)
);
}
/**
* Implements hook "mail.send"
* @param array $to
* @param string $subject
* @param string $message
* @param array $options
* @param mixed $result
*/
public function hookMailSend($to, $subject, $message, $options, &$result)
{
$this->setMailer($to, $subject, $message, $options, $result);
}
/**
* Send an E-mail
* @param array $to
* @param string $subject
* @param string $message
* @param array $options
* @param array $settings
* @return boolean|string
*/
public function send($to, $subject, $message, $options, $settings)
{
try {
$mailer = $this->getMailer();
} catch (Exception $ex) {
return $ex->getMessage();
}
$mailer->isSMTP();
$mailer->isHTML(!empty($options['html']));
$mailer->Body = $message;
$mailer->Subject = $subject;
if (!empty($options['html'])) {
$mailer->AltBody = strip_tags($message);
}
$mailer->Port = $settings['port'];
$mailer->setFrom($options['from']);
$mailer->Username = $settings['user'];
$mailer->Password = $settings['password'];
$mailer->SMTPSecure = $settings['secure'];
$mailer->SMTPAuth = !empty($settings['auth']);
$mailer->Host = implode(';', $settings['host']);
foreach ($to as $address) {
settype($address, 'array');
call_user_func_array(array($mailer, 'addAddress'), $address);
}
if (!empty($options['attachment'])) {
foreach ($options['attachment'] as $attachment) {
settype($attachment, 'array');
call_user_func_array(array($mailer, 'addAttachment'), $attachment);
}
}
if ($mailer->send()) {
return true;
}
return (string) $mailer->ErrorInfo;
}
/**
* Returns PHPMailer instance
* @return PHPMailer
* @throws LogicException
*/
public function getMailer()
{
$this->library->load('phpmailer');
if (class_exists('PHPMailer')) {
return new PHPMailer;
}
throw new LogicException('Class \PHPMailer not found');
}
/**
* @param array $to
* @param string $subject
* @param string $message
* @param array $options
* @param mixed $result
*/
protected function setMailer($to, $subject, $message, $options, &$result)
{
$settings = $this->module->getSettings('mail');
if (!empty($settings['status']) && $result === null) {
$sent = $this->send($to, $subject, $message, $options, $settings);
if ($sent === true) {
$result = true;
}
}
}
}