1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Courier; |
6
|
|
|
|
7
|
|
|
use Courier\Exceptions\TransmissionException; |
8
|
|
|
use Courier\Exceptions\UnsupportedContentException; |
9
|
|
|
use PhpEmail\Address; |
10
|
|
|
use PhpEmail\Attachment; |
11
|
|
|
use PhpEmail\Content\Contracts\SimpleContent; |
12
|
|
|
use PhpEmail\Content\Contracts\TemplatedContent; |
13
|
|
|
use PhpEmail\Email; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A Courier implementation the writes the information to a logger. |
18
|
|
|
* |
19
|
|
|
* This implementation is not designed to be used in a production system, but it can act as a drop-in testing implementation, |
20
|
|
|
* allowing developers to build content and log it to their local systems, without delivering emails. This can be |
21
|
|
|
* helpful in cases where the email might contain important information, like a generated password reset token. |
22
|
|
|
*/ |
23
|
|
|
class LoggingCourier implements Courier |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
|
30
|
2 |
|
public function __construct(LoggerInterface $logger) |
31
|
|
|
{ |
32
|
2 |
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Email $email |
37
|
|
|
* |
38
|
|
|
* @throws TransmissionException |
39
|
|
|
* @throws UnsupportedContentException |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
2 |
|
public function deliver(Email $email): void |
44
|
|
|
{ |
45
|
2 |
|
$this->logger->debug('Delivered email'); |
46
|
2 |
|
$this->logger->debug("Subject: {$email->getSubject()}"); |
47
|
2 |
|
$this->logger->debug("From: {$email->getFrom()->toRfc2822()}"); |
48
|
2 |
|
$this->logger->debug("Reply To: {$this->mapAddresses($email->getReplyTos())}"); |
49
|
2 |
|
$this->logger->debug("To: {$this->mapAddresses($email->getToRecipients())}"); |
50
|
2 |
|
$this->logger->debug("CC: {$this->mapAddresses($email->getCcRecipients())}"); |
51
|
2 |
|
$this->logger->debug("BCC: {$this->mapAddresses($email->getBccRecipients())}"); |
52
|
|
|
|
53
|
2 |
|
$this->logHeaders($email); |
54
|
2 |
|
$this->logAttachments($email); |
55
|
2 |
|
$this->logContent($email); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function mapAddresses(array $addresses): string |
59
|
|
|
{ |
60
|
2 |
|
return implode(', ', array_map(function (Address $address) { |
61
|
2 |
|
return $address->toRfc2822(); |
62
|
2 |
|
}, $addresses)); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
private function logHeaders(Email $email): void |
66
|
|
|
{ |
67
|
2 |
|
foreach ($email->getHeaders() as $header) { |
68
|
1 |
|
$this->logger->debug("{$header->getField()}: {$header->getValue()}"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function logAttachments(Email $email): void |
73
|
|
|
{ |
74
|
2 |
|
$attachmentNames = implode(', ', array_map(function (Attachment $attachment) { |
75
|
1 |
|
return $attachment->getName(); |
76
|
2 |
|
}, $email->getAttachments())); |
77
|
|
|
|
78
|
2 |
|
$embeddedNames = implode(', ', array_map(function (Attachment $attachment) { |
79
|
1 |
|
return $attachment->getName(); |
80
|
2 |
|
}, $email->getAttachments())); |
81
|
|
|
|
82
|
2 |
|
$this->logger->debug("Attaching: $attachmentNames"); |
83
|
2 |
|
$this->logger->debug("Embedding: $embeddedNames"); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
private function logContent(Email $email): void |
87
|
|
|
{ |
88
|
2 |
|
$content = $email->getContent(); |
89
|
2 |
|
if ($content instanceof TemplatedContent) { |
90
|
1 |
|
$this->logger->debug("Template ID: {$content->getTemplateId()}"); |
91
|
1 |
|
$this->logger->debug("Template Data:\n" . json_encode($content->getTemplateData(), JSON_PRETTY_PRINT)); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
if ($content instanceof SimpleContent) { |
95
|
1 |
|
$this->logger->debug("HTML:\n{$content->getHtml()->getBody()}"); |
96
|
1 |
|
$this->logger->debug("Text:\n{$content->getText()->getBody()}"); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|