Passed
Push — main ( 577f5e...2e91a7 )
by Dimitri
02:59
created

PHPMailer::dkim()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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