| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\test;
|
|
|
18 |
|
|
|
19 |
use stdClass;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Mailpit message handling implementation.
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright Simey Lameze <simey@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class mailpit_message implements message {
|
|
|
30 |
|
|
|
31 |
/** @var bool Whether the message has been loaded. */
|
|
|
32 |
protected bool $messageloaded = false;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Constructor.
|
|
|
36 |
*
|
|
|
37 |
* @param email_catcher $client The email catcher client.
|
|
|
38 |
* @param string $id The message ID.
|
|
|
39 |
* @param stdClass $sender The sender.
|
|
|
40 |
* @param string $subject The subject.
|
|
|
41 |
* @param array $recipients The recipients.
|
|
|
42 |
* @param array $cc The cc recipients.
|
|
|
43 |
* @param array $bcc The bcc recipients.
|
|
|
44 |
* @param int $attachmentcount The number of attachments.
|
|
|
45 |
* @param string|null $text The text body.
|
|
|
46 |
* @param string|null $html The HTML body.
|
|
|
47 |
* @param array $attachments The attachments.
|
|
|
48 |
* @param array $inline The inline attachments.
|
|
|
49 |
*/
|
|
|
50 |
protected function __construct(
|
|
|
51 |
/** @var email_catcher $client The email catcher client used for message operations.*/
|
|
|
52 |
private readonly email_catcher $client,
|
|
|
53 |
/** @var string $id The unique identifier for the message. */
|
|
|
54 |
private readonly string $id,
|
|
|
55 |
/** @var stdClass $sender The sender of the message, represented as an object with email details. */
|
|
|
56 |
private readonly stdClass $sender,
|
|
|
57 |
/** @var string $subject The subject line of the message. */
|
|
|
58 |
private readonly string $subject,
|
|
|
59 |
/** @var array $recipients List of primary recipients for the message. */
|
|
|
60 |
private readonly array $recipients,
|
|
|
61 |
/** @var array $cc List of carbon copy recipients (optional, defaults to an empty array). */
|
|
|
62 |
private readonly array $cc = [],
|
|
|
63 |
/** @var array $bcc List of blind carbon copy recipients (optional, defaults to an empty array). */
|
|
|
64 |
private readonly array $bcc = [],
|
|
|
65 |
/** @var int $attachmentcount The number of attachments in the message (default is 0). */
|
|
|
66 |
private readonly int $attachmentcount = 0,
|
|
|
67 |
/** @var ?string $text The plain text body of the message (nullable, might be loaded later). */
|
|
|
68 |
private ?string $text = null,
|
|
|
69 |
/** @var ?string $html The HTML body of the message (nullable, might be loaded later). */
|
|
|
70 |
private ?string $html = null,
|
|
|
71 |
/** @var array $attachments An array of attachment details (defaults to empty array). */
|
|
|
72 |
private array $attachments = [],
|
|
|
73 |
/** @var array $inline An array of inline elements like images or styles (defaults to empty array). */
|
|
|
74 |
private array $inline = [],
|
|
|
75 |
) {
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Load the message content.
|
|
|
80 |
*/
|
|
|
81 |
protected function load_message_content(): void {
|
|
|
82 |
if (!$this->messageloaded) {
|
|
|
83 |
$message = $this->client->get_message_data($this->id);
|
|
|
84 |
$this->text = $message->Text;
|
|
|
85 |
$this->html = $message->HTML;
|
|
|
86 |
$this->attachments = $message->Attachments;
|
|
|
87 |
$this->inline = $message->Inline;
|
|
|
88 |
$this->messageloaded = true;
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Create a message from an api response.
|
|
|
94 |
*
|
|
|
95 |
* @param email_catcher $client The email catcher client.
|
|
|
96 |
* @param stdClass $message The api response.
|
|
|
97 |
* @param bool $showdetails Optional. Whether to include detailed information in the messages. Default is false.
|
|
|
98 |
* @return mailpit_message The created mailpit message instance.
|
|
|
99 |
*/
|
|
|
100 |
public static function create_from_api_response(
|
|
|
101 |
email_catcher $client,
|
|
|
102 |
stdClass $message,
|
|
|
103 |
bool $showdetails = false,
|
|
|
104 |
): self {
|
|
|
105 |
$message = new self(
|
|
|
106 |
client: $client,
|
|
|
107 |
id: $message->ID,
|
|
|
108 |
sender: $message->From,
|
|
|
109 |
subject: $message->Subject,
|
|
|
110 |
recipients: $message->To,
|
|
|
111 |
cc: $message->Cc,
|
|
|
112 |
attachmentcount: $message->Attachments,
|
|
|
113 |
);
|
|
|
114 |
|
|
|
115 |
if ($showdetails) {
|
|
|
116 |
$message->load_message_content();
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return $message;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Get the text representation of the body, if one was provided.
|
|
|
124 |
*
|
|
|
125 |
* @return null|string The text body.
|
|
|
126 |
*/
|
|
|
127 |
public function get_body_text(): ?string {
|
|
|
128 |
return $this->text;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Get the HTML representation of the body, if one was provided.
|
|
|
133 |
*
|
|
|
134 |
* @return null|string The HTML body.
|
|
|
135 |
*/
|
|
|
136 |
public function get_body_html(): ?string {
|
|
|
137 |
return $this->html;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the message ID.
|
|
|
142 |
*
|
|
|
143 |
* @return string The ID.
|
|
|
144 |
*/
|
|
|
145 |
public function get_id(): string {
|
|
|
146 |
return $this->id;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Get the message recipients.
|
|
|
151 |
*
|
|
|
152 |
* @return iterable The recipients.
|
|
|
153 |
*/
|
|
|
154 |
public function get_recipients(): iterable {
|
|
|
155 |
foreach ($this->recipients as $user) {
|
|
|
156 |
yield mailpit_message_user::from_recipient($user);
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Get the first recipient of the message.
|
|
|
162 |
*
|
|
|
163 |
* @return string The email address of the first recipient.
|
|
|
164 |
*/
|
|
|
165 |
public function get_first_recipient(): string {
|
|
|
166 |
$recipients = $this->get_recipients();
|
|
|
167 |
foreach ($recipients as $recipient) {
|
|
|
168 |
return $recipient->get_address();
|
|
|
169 |
}
|
|
|
170 |
return '';
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Whether the message has the specified recipient.
|
|
|
175 |
*
|
|
|
176 |
* @param string $email The email address.
|
|
|
177 |
* @return bool Whether the message has the recipient.
|
|
|
178 |
*/
|
|
|
179 |
public function has_recipient(string $email): bool {
|
|
|
180 |
foreach ($this->get_recipients() as $recipient) {
|
|
|
181 |
if ($recipient->get_address() === $email) {
|
|
|
182 |
return true;
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
return false;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Get the message cc recipients.
|
|
|
190 |
*
|
|
|
191 |
* @return iterable The cc recipients.
|
|
|
192 |
*/
|
|
|
193 |
public function get_cc(): iterable {
|
|
|
194 |
foreach ($this->cc as $user) {
|
|
|
195 |
yield mailpit_message_user::from_recipient($user);
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Get the message bcc recipients.
|
|
|
201 |
*
|
|
|
202 |
* @return iterable The bcc recipients.
|
|
|
203 |
*/
|
|
|
204 |
public function get_bcc(): iterable {
|
|
|
205 |
foreach ($this->bcc as $user) {
|
|
|
206 |
yield mailpit_message_user::from_recipient($user);
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Get the message subject.
|
|
|
212 |
*
|
|
|
213 |
* @return string The subject.
|
|
|
214 |
*/
|
|
|
215 |
public function get_subject(): string {
|
|
|
216 |
return $this->subject;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Get the message sender.
|
|
|
221 |
*
|
|
|
222 |
* @return message_user The sender.
|
|
|
223 |
*/
|
|
|
224 |
public function get_sender(): message_user {
|
|
|
225 |
return mailpit_message_user::from_sender($this->sender);
|
|
|
226 |
}
|
|
|
227 |
}
|