| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\Mail\ImapEngine; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Core; |
| 26 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 27 |
use Liuch\DmarcSrg\Exception\MailboxException; |
| 28 |
|
| 29 |
use DirectoryTree\ImapEngine\Mailbox as IMailbox; |
| 30 |
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; |
| 31 |
use DirectoryTree\ImapEngine\Exceptions\Exception as IException; |
| 32 |
|
| 33 |
class MailMessage extends \Liuch\DmarcSrg\Mail\MailMessage |
| 34 |
{ |
| 35 |
private $message; |
| 36 |
private $mailbox; |
| 37 |
|
| 38 |
public function __construct($data) |
| 39 |
{ |
| 40 |
$this->message = $data['message']; |
| 41 |
$this->mailbox = $data['mailbox']; |
| 42 |
} |
| 43 |
|
| 44 |
public function overview(): array |
| 45 |
{ |
| 46 |
$res = []; |
| 47 |
if (($from = $this->message->from())) { |
| 48 |
$res['from'] = $from->name(); |
| 49 |
} |
| 50 |
if (($date = $this->message->date())) { |
| 51 |
$res['date'] = $date; |
| 52 |
} |
| 53 |
return $res; |
| 54 |
} |
| 55 |
|
| 56 |
public function markSeen(): void |
| 57 |
{ |
| 58 |
try { |
| 59 |
$this->message->markSeen(); |
| 60 |
} catch (IException $e) { |
| 61 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 62 |
throw new MailboxException('IMAP: Cannot mark a message', -1, $e); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function attachment() |
| 67 |
{ |
| 68 |
$this->ensureAttachment(); |
| 69 |
return $this->attachment; |
| 70 |
} |
| 71 |
|
| 72 |
public function move(string $target_folder): void |
| 73 |
{ |
| 74 |
try { |
| 75 |
$current_folder = $this->message->folder(); |
| 76 |
$target_path = $current_folder->path() . $current_folder->delimiter() . $target_folder; |
| 77 |
$this->message->move($target_path, false); |
| 78 |
$this->mailbox->setExpunge(); |
| 79 |
} catch (IException $e) { |
| 80 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 81 |
throw new MailboxException('IMAP: Cannot move a message', -1, $e); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function delete(): void |
| 86 |
{ |
| 87 |
try { |
| 88 |
$this->message->delete(false); |
| 89 |
$this->mailbox->setExpunge(); |
| 90 |
} catch (IException $e) { |
| 91 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 92 |
throw new MailboxException('IMAP: Cannot delete a message', -1, $e); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
protected function attachmentCount(): int |
| 97 |
{ |
| 98 |
$this->ensureBody(); |
| 99 |
return $this->message->attachmentCount(); |
| 100 |
} |
| 101 |
|
| 102 |
protected function ensureAttachment(): void |
| 103 |
{ |
| 104 |
$this->ensureBody(); |
| 105 |
$alist = $this->message->attachments(); |
| 106 |
if (!isset($alist[0])) { |
| 107 |
throw new SoftException('The message has no attachment'); |
| 108 |
} |
| 109 |
$this->attachment = new MailAttachment($alist[0]); |
| 110 |
} |
| 111 |
|
| 112 |
private function ensureBody(): void |
| 113 |
{ |
| 114 |
if (!$this->message->hasBody()) { |
| 115 |
$msg = $this->message->folder()->messages() |
| 116 |
->withHeaders()->withBody() |
| 117 |
->find($this->message->uid(), ImapFetchIdentifier::Uid); |
| 118 |
if (!$msg) { |
| 119 |
throw new SoftException('The message not found'); |
| 120 |
} |
| 121 |
$this->message = $msg; |
| 122 |
} |
| 123 |
} |
| 124 |
} |