Debian Codegraph @

classes/Mail/ImapEngine/MailMessage.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
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 }