1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Mail; |
15
|
|
|
|
16
|
|
|
use PHPMailer\PHPMailer\Exception; |
17
|
|
|
use PHPMailer\PHPMailer\PHPMailer as PHPMailerClient; |
18
|
|
|
use Valkyrja\Mail\Contract\Mailer as Contract; |
19
|
|
|
use Valkyrja\Mail\Data\Contract\Message; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class PhpMailer. |
23
|
|
|
* |
24
|
|
|
* @author Melech Mizrachi |
25
|
|
|
*/ |
26
|
|
|
class PhpMailer implements Contract |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* PhpMailer constructor. |
30
|
|
|
* |
31
|
|
|
* @param PHPMailerClient $phpMailer |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
protected PHPMailerClient $phpMailer |
35
|
|
|
) { |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
public function send(Message $message): void |
44
|
|
|
{ |
45
|
|
|
$this->phpMailer->setFrom($message->getFromEmail(), $message->getFromName()); |
46
|
|
|
|
47
|
|
|
$this->addRecipients('addAddress', $message->getRecipients()); |
48
|
|
|
$this->addRecipients('addReplyTo', $message->getReplyToRecipients()); |
49
|
|
|
$this->addRecipients('addCC', $message->getCopyRecipients()); |
50
|
|
|
$this->addRecipients('addBCC', $message->getBlindCopyRecipients()); |
51
|
|
|
$this->addAttachments($message->getAttachments()); |
52
|
|
|
$this->addPlainBody($message->getPlainBody()); |
53
|
|
|
|
54
|
|
|
$this->phpMailer->Subject = $message->getSubject(); |
55
|
|
|
$this->phpMailer->Body = $message->getBody(); |
56
|
|
|
$this->phpMailer->isHTML($message->isHtml()); |
57
|
|
|
|
58
|
|
|
$this->phpMailer->send(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add recipients to PHP Mailer by method. |
63
|
|
|
* |
64
|
|
|
* @param string $method The phpMailer method to call |
65
|
|
|
* @param array<int, array{email: string, name: string}> $recipients The recipients |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function addRecipients(string $method, array $recipients): void |
70
|
|
|
{ |
71
|
|
|
foreach ($recipients as $recipient) { |
72
|
|
|
$this->phpMailer->{$method}($recipient['email'], $recipient['name']); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add attachments to PHP Mailer. |
78
|
|
|
* |
79
|
|
|
* @param array<int, array{path: string, name: string}> $attachments The attachments |
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function addAttachments(array $attachments): void |
86
|
|
|
{ |
87
|
|
|
foreach ($attachments as $attachment) { |
88
|
|
|
$this->phpMailer->addAttachment($attachment['path'], $attachment['name']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add plain body to PHP Mailer. |
94
|
|
|
* |
95
|
|
|
* @param string|null $plainBody |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
protected function addPlainBody(string|null $plainBody = null): void |
100
|
|
|
{ |
101
|
|
|
if ($plainBody !== null) { |
102
|
|
|
$this->phpMailer->AltBody = $plainBody; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|