Skip to content

Commit 5548db5

Browse files
committed
Restore a message from a local or remote file
1 parent 96f6ca6 commit 5548db5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Message.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,48 @@ public static function make(int $uid, ?int $msglist, Client $client, string $raw
291291
return $instance;
292292
}
293293

294+
/**
295+
* Create a new message instance by reading and loading a file or remote location
296+
*
297+
* @throws RuntimeException
298+
* @throws MessageContentFetchingException
299+
* @throws ResponseException
300+
* @throws ImapBadRequestException
301+
* @throws InvalidMessageDateException
302+
* @throws ConnectionFailedException
303+
* @throws ImapServerErrorException
304+
* @throws ReflectionException
305+
* @throws AuthFailedException
306+
* @throws MaskNotFoundException
307+
*/
308+
public static function fromFile($filename): Message {
309+
$reflection = new ReflectionClass(self::class);
310+
/** @var Message $instance */
311+
$instance = $reflection->newInstanceWithoutConstructor();
312+
$instance->boot();
313+
314+
$default_mask = ClientManager::getMask("message");
315+
if($default_mask != ""){
316+
$instance->setMask($default_mask);
317+
}else{
318+
throw new MaskNotFoundException("Unknown message mask provided");
319+
}
320+
321+
$email = file_get_contents($filename);
322+
if(!str_contains($email, "\r\n")){
323+
$email = str_replace("\n", "\r\n", $email);
324+
}
325+
$raw_header = substr($email, 0, strpos($email, "\r\n\r\n"));
326+
$raw_body = substr($email, strlen($raw_header)+8);
327+
328+
$instance->parseRawHeader($raw_header);
329+
$instance->parseRawBody($raw_body);
330+
331+
$instance->setUid(0);
332+
333+
return $instance;
334+
}
335+
294336
/**
295337
* Boot a new instance
296338
*/

0 commit comments

Comments
 (0)