Completed
Push — master ( 3a892f...986925 )
by Iurii
01:03
created

Mail::getMailerInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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\Module;
13
14
/**
15
 * Main class for Mail module
16
 */
17
class Mail extends Module
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Implements hook "library.list"
30
     * @param array $libraries
31
     */
32
    public function hookLibraryList(array &$libraries)
33
    {
34
        $libraries['phpmailer'] = array(
35
            'name' => 'PHP Mail',
36
            'description' => 'The classic email sending library for PHP',
37
            'url' => 'https://github.com/PHPMailer/PHPMailer',
38
            'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip',
39
            'type' => 'php',
40
            'version_source' => array(
41
                'lines' => 100,
42
                'pattern' => '/.*\\$Version.*(\\d+\\.+\\d+\\.+\\d+)/',
43
                'file' => 'vendor/phpmailer/phpmailer/class.phpmailer.php'
44
            ),
45
            'module' => 'mail',
46
            'files' => array(
47
                'vendor/autoload.php'
48
            )
49
        );
50
    }
51
52
    /**
53
     * Implements hook "route.list"
54
     * @param array $routes
55
     */
56
    public function hookRouteList(array &$routes)
57
    {
58
        $routes['admin/module/settings/mail'] = array(
59
            'access' => 'module_edit',
60
            'handlers' => array(
61
                'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings')
62
            )
63
        );
64
    }
65
66
    /**
67
     * Implements hook "mail.send"
68
     * @param array $to
69
     * @param string $subject
70
     * @param string $message
71
     * @param array $options
72
     * @param mixed $result
73
     */
74
    public function hookMailSend($to, $subject, $message, $options, &$result)
75
    {
76
        $settings = $this->config->module('mail');
77
78
        // Check if the module enabled AND result is null, i.e not caught by another module
79
        if (!empty($settings['status']) && $result === null) {
80
            $sent = $this->send($to, $subject, $message, $options, $settings);
81
            if ($sent === true) {
82
                // On success override $result argument to mark the massage was sent
83
                // Another modules implementing this hook won't send this message again
84
                $result = true;
85
            }
86
        }
87
    }
88
89
    /**
90
     * Returns PHPMailer instance
91
     * @return \PHPMailer
92
     * @throws \InvalidArgumentException
93
     */
94
    protected function getMailerInstance()
95
    {
96
        $this->getLibrary()->load('phpmailer');
97
98
        if (class_exists('PHPMailer')) {
99
            return new \PHPMailer;
100
        }
101
102
        throw new \InvalidArgumentException('Class PHPMailer not found');
103
    }
104
105
    /**
106
     * Send an E-mail
107
     * @param array $to
108
     * @param string $subject
109
     * @param string $message
110
     * @param array $options
111
     * @param array $settings
112
     * @return boolean|string
113
     */
114
    public function send($to, $subject, $message, $options, $settings)
115
    {
116
        try {
117
            $mailer = $this->getMailerInstance();
118
        } catch (\InvalidArgumentException $ex) {
119
            return $ex->getMessage();
120
        }
121
122
        $mailer->isSMTP();
123
        $mailer->isHTML(!empty($options['html']));
124
125
        $mailer->Body = $message;
126
        $mailer->Subject = $subject;
127
128
        if (!empty($options['html'])) {
129
            $mailer->AltBody = strip_tags($message);
130
        }
131
132
        $mailer->Port = $settings['port'];
133
        $mailer->setFrom($options['from']);
134
        $mailer->Username = $settings['user'];
135
        $mailer->Password = $settings['password'];
136
        $mailer->SMTPSecure = $settings['secure'];
137
        $mailer->SMTPAuth = !empty($settings['auth']);
138
        $mailer->Host = implode(';', $settings['host']);
139
140
        foreach ($to as $address) {
141
            settype($address, 'array');
142
            call_user_func_array(array($mailer, 'addAddress'), $address);
143
        }
144
145
        if (!empty($options['attachment'])) {
146
            foreach ($options['attachment'] as $attachment) {
147
                settype($attachment, 'array');
148
                call_user_func_array(array($mailer, 'addAttachment'), $attachment);
149
            }
150
        }
151
152
        if ($mailer->send()) {
153
            return true;
154
        }
155
156
        return (string) $mailer->ErrorInfo;
157
    }
158
159
    /**
160
     * Implements hook "module.enable.after"
161
     */
162
    public function hookModuleEnableAfter()
163
    {
164
        $this->getLibrary()->clearCache();
165
    }
166
167
    /**
168
     * Implements hook "module.disable.after"
169
     */
170
    public function hookModuleDisableAfter()
171
    {
172
        $this->getLibrary()->clearCache();
173
    }
174
175
    /**
176
     * Implements hook "module.install.after"
177
     */
178
    public function hookModuleInstallAfter()
179
    {
180
        $this->getLibrary()->clearCache();
181
    }
182
183
    /**
184
     * Implements hook "module.uninstall.after"
185
     */
186
    public function hookModuleUninstallAfter()
187
    {
188
        $this->getLibrary()->clearCache();
189
    }
190
191
}
192