Passed
Push — main ( 7ca792...577f5e )
by Dimitri
03:34
created

PHPMailer::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace BlitzPHP\Mail\Adapters;
4
5
use PHPMailer\PHPMailer\PHPMailer as Mailer;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\PHPMailer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use PHPMailer\PHPMailer\SMTP;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\SMTP was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class PHPMailer extends AbstractAdapter
9
{
10
    /**
11
     * {@inheritDoc}
12
     */
13
    protected array $dependancies = [
14
        ['class' => Mailer::class, 'package' => 'phpmailer/phpmailer']
15
    ];
16
17
    /**
18
	 * @var Mailer
19
	 */
20
    protected $mailer;
21
22
    public function __construct(bool $debug = false)
23
    {
24
        parent::__construct($debug);
25
26
        $this->mailer = new Mailer();
27
        $this->mailer->SMTPAuth   = true;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function setPort(int $port): self 
34
    {
35
        $this->mailer->Port = $port;
36
37
        return $this;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function setHost(string $host): self 
44
    {
45
        $this->mailer->Host = $host;
46
47
        return $this;
48
    }
49
    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function setUsername(string $username): self 
54
    {
55
        $this->mailer->Username = $username;
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function setPassword(string $password): self 
64
    {
65
        $this->mailer->Password = $password;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function setDebug(int $debug = SMTP::DEBUG_SERVER): self 
74
    {
75
        $this->mailer->SMTPDebug = $debug;
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function setProtocol(string $protocol): self 
84
    {
85
        switch (strtolower($protocol)) {
86
            case static::PROTOCOL_MAIL:
87
                $this->mailer->isMail();
88
                break;
89
            case static::PROTOCOL_QMAIL:
90
                $this->mailer->isQmail();
91
                break;
92
            case static::PROTOCOL_SENDMAIL:
93
                $this->mailer->isSendmail();
94
                break;
95
            default:
96
                $this->mailer->isSMTP();
97
                break;
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function setTimeout(int $timeout): self 
107
    {
108
        $this->mailer->Timeout = $timeout;
109
110
        return $this;
111
    }
112
    
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function setCharset(string $charset): self 
117
    {
118
        $this->mailer->CharSet = $charset;
119
120
        return $this;
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126
    public function setPriority(int $priority): self 
127
    {
128
        if (in_array($priority, static::PRIORITY_MAP, true)) {
129
            $this->mailer->Priority = $priority;
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function setEncryption(?string $encryption): self 
139
    {
140
        if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) {
141
            $this->mailer->SMTPSecure = $encryption;
142
        }
143
144
        return $this;
145
    }
146
147
148
    /**
149
	 * {@inheritDoc}
150
     */
151
    public function alt(string $content) : self
152
    {
153
        $this->mailer->AltBody = $content;
154
155
        return $this;
156
    }
157
158
    /**
159
	 * {@inheritDoc}
160
	 *
161
     * @throws \PHPMailer\PHPMailer\Exception
162
     */
163
    public function attachment(array|string $path, string $name = '', string $encoding = Mailer::ENCODING_BASE64, string $disposition = 'attachment'): self
164
    {
165
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
166
            $path = [$path => $name];
167
        }
168
169
        foreach ($path As $key => $value) {
170
            $this->mailer->addAttachment($key, $value, $encoding, '', $disposition);
171
        }
172
173
        return $this;
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     * 
179
     * @throws \PHPMailer\PHPMailer\Exception
180
     */
181
    public function bcc(array|string $address, bool|string $name = '', bool $set = false): self
182
    {
183
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
184
185
        if ($set) {
186
            $this->mailer->clearBCCs();
187
        } 
188
        
189
        foreach ($addresses as $address) {
190
            $this->mailer->addBCC(...$address);
191
        }
192
193
        return $this;
194
    }
195
196
    /**
197
	 * {@inheritDoc}
198
	 *
199
     * @throws \PHPMailer\PHPMailer\Exception
200
     */
201
    public function cc(array|string $address, bool|string $name = '', bool $set = false): self
202
    {
203
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
204
205
        if ($set) {
206
            $this->mailer->clearCCs();
207
        } 
208
        
209
        foreach ($addresses as $address) {
210
            $this->mailer->addCC(...$address);
211
        }
212
        
213
        return $this;
214
    }
215
216
    /**
217
	 * {@inheritDoc}
218
	 *
219
     * @throws \PHPMailer\PHPMailer\Exception
220
     */
221
    public function from(string $address, string $name = '') : self
222
    {
223
        $this->mailer->setFrom($address, $name);
224
      
225
        return $this;
226
    }
227
228
    /**
229
	 * {@inheritDoc}
230
	 *
231
     * @throws \PHPMailer\PHPMailer\Exception
232
     */
233
    public function header(array|string $name, ?string $value = null) : self
234
    {
235
        if (is_string($name)) {
0 ignored issues
show
introduced by
The condition is_string($name) is always false.
Loading history...
236
            $name = [$name => $value];
237
        }
238
239
        foreach ($name As $key => $value) {
240
            $this->mailer->addCustomHeader($key, $value);
241
        }
242
        
243
        return $this;
244
    }
245
246
    /**
247
     * {@inheritDoc}
248
     */
249
    public function html(string $content): self
250
    {
251
        $this->mailer->isHTML(true);
252
        
253
        return $this->message($content);
254
    }
255
    
256
    /**
257
	 * {@inheritDoc}
258
     */
259
    public function lastId(): string
260
    {
261
        return $this->mailer->getLastMessageID();
262
    }
263
    
264
    /**
265
	 * {@inheritDoc}
266
     */
267
    public function message(string $message): self
268
    {
269
        $this->mailer->Body = $message;
270
271
        return $this;
272
    }
273
274
    /**
275
	 * {@inheritDoc}
276
	 *
277
     * @throws \PHPMailer\PHPMailer\Exception
278
     */
279
    public function replyTo(array|string $address, bool|string $name = '', bool $set = false): self
280
    {
281
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
282
283
        if ($set) {
284
            $this->mailer->clearReplyTos();
285
        } 
286
        
287
        foreach ($addresses as $address) {
288
            $this->mailer->addReplyTo(...$address);
289
        }
290
291
        return $this;
292
    }
293
294
    /**
295
     * {@inheritDoc}
296
     * 
297
     * @throws \PHPMailer\PHPMailer\Exception
298
     */
299
    public function send(): bool
300
    {
301
        return $this->mailer->send();
302
    }
303
304
    public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): self
305
    {
306
        $this->mailer->sign($cert_filename, $key_filename, $key_pass, $extracerts_filename);
307
308
        return $this;
309
    }
310
311
    /**
312
	 * {@inheritDoc}
313
     */
314
    public function subject(string $subject): self
315
    {
316
        $this->mailer->Subject = $subject;
317
318
        return $this;
319
    }
320
321
    /**
322
     * {@inheritDoc}
323
     */
324
    public function text(string $content): self
325
    {
326
        $this->mailer->isHTML(false);
327
        
328
        return $this->message($content);
329
    }
330
331
    /**
332
	 * {@inheritDoc}
333
	 *
334
     * @throws \PHPMailer\PHPMailer\Exception
335
     */
336
    public function to(array|string $address, bool|string $name = '', bool $set = false): self
337
    {
338
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
339
340
        if ($set) {
341
            $this->mailer->clearAddresses();
342
        } 
343
        
344
        foreach ($addresses as $address) {
345
            $this->mailer->addAddress(...$address);
346
        }
347
348
        return $this;
349
    }
350
    
351
352
    /**
353
     * {@inheritDoc}
354
     * 
355
     * @return array
356
     */
357
    protected function makeAddress(string $email, string $name) 
358
    {
359
        if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
360
            $tmp = $email;
361
            $email = $name;
362
            $name = $tmp;
363
        }
364
365
        return [$email, $name];
366
    }
367
}