Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 18 |
CRAP | |
0.00% |
0 / 1 |
| Imap | |
0.00% |
0 / 65 |
|
0.00% |
0 / 18 |
992 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| __destruct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| connectInbox | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getBoxes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| renameBox | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| deleteBox | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| createBox | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| countMail | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getMailboxInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| countRecent | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| countUnseen | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| search | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| copyMail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| moveMail | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| deleteMail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaders | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getHeaderInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| closeInbox | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Message\Mail |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Message\Mail; |
| 16 | |
| 17 | /** |
| 18 | * Imap mail class. |
| 19 | * |
| 20 | * @package phpOMS\Message\Mail |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Imap implements MailBoxInterface |
| 26 | { |
| 27 | /** |
| 28 | * Connection flags |
| 29 | * |
| 30 | * @var string |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | public string $flags = '/imap'; |
| 34 | |
| 35 | /** |
| 36 | * Current inbox |
| 37 | * |
| 38 | * Boxes can be in parent boxes. The path must be delimitted with . e.g. INBOX.Subdir1.Subdir2 |
| 39 | * |
| 40 | * @var string |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | private $box = null; |
| 44 | |
| 45 | /** |
| 46 | * Host. |
| 47 | * |
| 48 | * @var string |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | public string $host = 'localhost'; |
| 52 | |
| 53 | /** |
| 54 | * The default port. |
| 55 | * |
| 56 | * @var int |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | public int $port = 110; |
| 60 | |
| 61 | /** |
| 62 | * Encryption |
| 63 | * |
| 64 | * @var string |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public string $encryption = EncryptionType::NONE; |
| 68 | |
| 69 | /** |
| 70 | * Username. |
| 71 | * |
| 72 | * @var string |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public string $username = ''; |
| 76 | |
| 77 | /** |
| 78 | * Password. |
| 79 | * |
| 80 | * @var string |
| 81 | * @since 1.0.0 |
| 82 | */ |
| 83 | public string $password = ''; |
| 84 | |
| 85 | /** |
| 86 | * {@inheritdoc} |
| 87 | */ |
| 88 | public function __construct(string $user = '', string $pass = '', int $port = 143, string $encryption = EncryptionType::NONE) |
| 89 | { |
| 90 | $this->username = $user; |
| 91 | $this->password = $pass; |
| 92 | $this->port = $port; |
| 93 | $this->encryption = $encryption; |
| 94 | $this->flags .= $this->encryption !== EncryptionType::NONE ? '/ssl' : ''; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Destructor. |
| 99 | * |
| 100 | * @since 1.0.0 |
| 101 | * @codeCoverageIgnore |
| 102 | */ |
| 103 | public function __destruct() |
| 104 | { |
| 105 | $this->closeInbox(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * {@inheritdoc} |
| 110 | */ |
| 111 | public function connectInbox() : bool |
| 112 | { |
| 113 | $this->mailbox = ($tmp = @\imap_open( |
| 114 | '{' . $this->host . ':' . $this->port . $this->flags . '}', |
| 115 | $this->username, $this->password |
| 116 | )) === false ? null : $tmp; |
| 117 | |
| 118 | return \is_resource($this->mailbox); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get mailboxes |
| 123 | * |
| 124 | * @return array |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | */ |
| 128 | public function getBoxes() : array |
| 129 | { |
| 130 | $list = \imap_list( |
| 131 | $this->mailbox, |
| 132 | $reference = '{' . $this->host . ':' . $this->port . '}', |
| 133 | '*' |
| 134 | ); |
| 135 | if (!\is_array($list)) { |
| 136 | return []; // @codeCoverageIgnore |
| 137 | } |
| 138 | |
| 139 | foreach ($list as $key => $value) { |
| 140 | $list[$key] = \str_replace($reference, '', \imap_utf7_decode($value)); |
| 141 | } |
| 142 | |
| 143 | return $list; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Rename mailbox |
| 148 | * |
| 149 | * @param string $old Old name |
| 150 | * @param string $new New name |
| 151 | * |
| 152 | * @return bool |
| 153 | * |
| 154 | * @since 1.0.0 |
| 155 | */ |
| 156 | public function renameBox(string $old, string $new) : bool |
| 157 | { |
| 158 | return \imap_renamemailbox( |
| 159 | $this->mailbox, |
| 160 | \imap_utf7_encode('{' . $this->host . ':' . $this->port . '}' . $old), |
| 161 | \imap_utf7_encode('{' . $this->host . ':' . $this->port . '}' . $new) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Delete mailbox |
| 167 | * |
| 168 | * @param string $box Box to delete |
| 169 | * |
| 170 | * @return bool |
| 171 | * |
| 172 | * @since 1.0.0 |
| 173 | */ |
| 174 | public function deleteBox(string $box) : bool |
| 175 | { |
| 176 | return \imap_deletemailbox( |
| 177 | $this->mailbox, |
| 178 | \imap_utf7_encode('{' . $this->host . ':' . $this->port . '}' . $box) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Create mailbox |
| 184 | * |
| 185 | * @param string $box Box to create |
| 186 | * |
| 187 | * @return bool |
| 188 | * |
| 189 | * @since 1.0.0 |
| 190 | */ |
| 191 | public function createBox(string $box) : bool |
| 192 | { |
| 193 | return \imap_createmailbox( |
| 194 | $this->mailbox, |
| 195 | \imap_utf7_encode('{' . $this->host . ':' . $this->port . '}' . $box) |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * {@inheritdoc} |
| 201 | */ |
| 202 | public function countMail(string $box) : int |
| 203 | { |
| 204 | if ($this->box !== $box) { |
| 205 | \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box); |
| 206 | $this->box = $box; |
| 207 | } |
| 208 | |
| 209 | return \imap_num_msg($this->mailbox); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * {@inheritdoc} |
| 214 | */ |
| 215 | public function getMailboxInfo(string $box) : object |
| 216 | { |
| 217 | return \imap_status($this->mailbox, '{' . $this->host . ':' . $this->port . '}' . $box, \SA_ALL); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * {@inheritdoc} |
| 222 | */ |
| 223 | public function countRecent(string $box) : int |
| 224 | { |
| 225 | if ($this->box !== $box) { |
| 226 | \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box); |
| 227 | $this->box = $box; |
| 228 | } |
| 229 | |
| 230 | return \imap_num_recent($this->mailbox); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * {@inheritdoc} |
| 235 | */ |
| 236 | public function countUnseen(string $box) : int |
| 237 | { |
| 238 | if ($this->box !== $box) { |
| 239 | \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box); |
| 240 | $this->box = $box; |
| 241 | } |
| 242 | |
| 243 | return \count(\imap_search($this->mailbox, 'UNSEEN')); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * {@inheritdoc} |
| 248 | */ |
| 249 | public function search( |
| 250 | string $box, |
| 251 | string $subject = '', |
| 252 | string $body = '', |
| 253 | string $to = '', |
| 254 | string $cc = '', |
| 255 | string $from = '', |
| 256 | string $bcc = '', |
| 257 | \DateTime $before = null, |
| 258 | \DateTime $since = null, |
| 259 | \DateTime $on = null, |
| 260 | bool $deleted = false, |
| 261 | bool $flagged = false |
| 262 | ) : array |
| 263 | { |
| 264 | if ($this->box !== $box) { |
| 265 | \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box); |
| 266 | $this->box = $box; |
| 267 | } |
| 268 | |
| 269 | return []; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Copy message to another mailbox |
| 274 | * |
| 275 | * @param string|array $messages Messages to copy |
| 276 | * @param string $box Box to copy messages to |
| 277 | * |
| 278 | * @return bool |
| 279 | * |
| 280 | * @since 1.0.0 |
| 281 | */ |
| 282 | public function copyMail(string | array $messages, string $box) : bool |
| 283 | { |
| 284 | return \imap_mail_copy($this->mailbox, \is_string($messages) ? $messages : \implode(',', $messages), '{' . $this->host . ':' . $this->port . '}' . $box); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Move message to another mailbox |
| 289 | * |
| 290 | * @param string|array $messages Messages to copy |
| 291 | * @param string $box Box to copy messages to |
| 292 | * |
| 293 | * @return bool |
| 294 | * |
| 295 | * @since 1.0.0 |
| 296 | */ |
| 297 | public function moveMail(string | array $messages, string $box) : bool |
| 298 | { |
| 299 | return \imap_mail_copy( |
| 300 | $this->mailbox, |
| 301 | \is_string($messages) |
| 302 | ? $messages |
| 303 | : \implode(',', $messages), '{' . $this->host . ':' . $this->port . '}' . $box |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Delete message |
| 309 | * |
| 310 | * @param int $msg Message number (not uid) |
| 311 | * |
| 312 | * @return bool |
| 313 | * |
| 314 | * @since 1.0.0 |
| 315 | */ |
| 316 | public function deleteMail(int $msg) : bool |
| 317 | { |
| 318 | return \imap_delete($this->mailbox, (string) $msg); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * {@inheritdoc} |
| 323 | */ |
| 324 | public function getHeaders(string $box) : array |
| 325 | { |
| 326 | if ($this->box !== $box) { |
| 327 | \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box); |
| 328 | $this->box = $box; |
| 329 | } |
| 330 | |
| 331 | return \imap_headers($this->mailbox); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Get message header information |
| 336 | * |
| 337 | * @param int $msg Message number (not uid) |
| 338 | * |
| 339 | * @return object |
| 340 | * |
| 341 | * @since 1.0.0 |
| 342 | */ |
| 343 | public function getHeaderInfo(int $msg) : object |
| 344 | { |
| 345 | return \imap_headerinfo($this->mailbox, $msg); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * {@inheritdoc} |
| 350 | */ |
| 351 | public function getMail(int $msg) : Email |
| 352 | { |
| 353 | return new Email(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * {@inheritdoc} |
| 358 | */ |
| 359 | public function closeInbox() : void |
| 360 | { |
| 361 | if (\is_resource($this->mailbox)) { |
| 362 | \imap_close($this->mailbox); |
| 363 | } |
| 364 | } |
| 365 | } |