-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMail.php
More file actions
118 lines (99 loc) · 3.51 KB
/
Mail.php
File metadata and controls
118 lines (99 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace ErrorHeroModule\Handler\Writer;
use Exception;
use Laminas\Log\Exception as LogException;
use Laminas\Log\Writer\Mail as BaseMail;
use Laminas\Mail\Header\ContentType;
use Laminas\Mail\Message as MailMessage;
use Laminas\Mail\Transport\TransportInterface;
use Laminas\Mime\Message as MimeMessage;
use Laminas\Mime\Mime;
use Laminas\Mime\Part;
use Override;
use function fopen;
use function implode;
use function is_array;
use function key;
use function sprintf;
use function trigger_error;
use const E_USER_WARNING;
use const PHP_EOL;
final class Mail extends BaseMail
{
private const string NAME = 'name';
/**
* @throws LogException\InvalidArgumentException
*/
public function __construct(
MailMessage $mailMessage,
TransportInterface $transport,
private readonly array $filesData
) {
parent::__construct($mailMessage, $transport);
}
/**
* {inheritDoc}
*
* Override with apply attachment whenever there is $_FILES data
*/
#[Override]
public function shutdown(): void
{
// Always provide events to mail as plaintext.
$body = implode(PHP_EOL, $this->eventsToMail);
if ($this->filesData === []) {
$this->mail->setBody($body);
} else {
$mimePart = new Part($body);
$mimePart->type = Mime::TYPE_TEXT;
$mimePart->charset = 'utf-8';
$mimePart->encoding = Mime::ENCODING_8BIT;
$body = new MimeMessage();
$body->addPart($mimePart);
$body = $this->bodyAddPart($body, $this->filesData);
$this->mail->setBody($body);
$headers = $this->mail->getHeaders();
/** @var ContentType $contentTypeHeader */
$contentTypeHeader = $headers->get('Content-Type');
$contentTypeHeader->setType('multipart/alternative');
}
// Finally, send the mail. If an exception occurs, convert it into a
// warning-level message so we can avoid an exception thrown without a
// stack frame.
try {
$this->transport->send($this->mail);
} catch (Exception $exception) {
$message = $exception->getMessage();
/** @var int $code */
$code = $exception->getCode();
trigger_error(
"unable to send log entries via email; "
. sprintf('message = %s; ', $message)
. sprintf('code = %d; ', $code)
. "exception class = " . $exception::class,
E_USER_WARNING
);
}
}
private function bodyAddPart(MimeMessage $mimeMessage, array $data): MimeMessage
{
foreach ($data as $singleData) {
if (key($singleData) === self::NAME && ! is_array($singleData[self::NAME])) {
$mimeMessage = $this->singleBodyAddPart($mimeMessage, $singleData);
continue;
}
$mimeMessage = $this->bodyAddPart($mimeMessage, $singleData);
}
return $mimeMessage;
}
private function singleBodyAddPart(MimeMessage $mimeMessage, array $data): MimeMessage
{
$mimePart = new Part(fopen($data['tmp_name'], 'r'));
$mimePart->type = $data['type'];
$mimePart->filename = $data[self::NAME];
$mimePart->disposition = Mime::DISPOSITION_ATTACHMENT;
$mimePart->encoding = Mime::ENCODING_BASE64;
return $mimeMessage->addPart($mimePart);
}
}