Skip to content

Commit a09a639

Browse files
committed
expunge option added to critical imap operations
1 parent d418f09 commit a09a639

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/IMAP/Client.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ public function setConfig(array $config) {
152152
/**
153153
* Get the current imap resource
154154
*
155-
* @return resource|boolean
155+
* @return bool|resource
156+
* @throws ConnectionFailedException
156157
*/
157158
public function getConnection() {
158159
$this->checkConnection();
@@ -324,38 +325,50 @@ public function openFolder(Folder $folder, $attempts = 3) {
324325
/**
325326
* Create a new Folder
326327
* @param string $name
328+
* @param boolean $expunge
327329
*
328330
* @return bool
329331
* @throws ConnectionFailedException
330332
*/
331-
public function createFolder($name) {
333+
public function createFolder($name, $expunge = true) {
332334
$this->checkConnection();
333-
return imap_createmailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
335+
$status = imap_createmailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
336+
if($expunge) $this->expunge();
337+
338+
return $status;
334339
}
335340

336341
/**
337342
* Rename Folder
338-
* @param string $old_name
339-
* @param string $new_name
343+
* @param string $old_name
344+
* @param string $new_name
345+
* @param boolean $expunge
340346
*
341347
* @return bool
342348
* @throws ConnectionFailedException
343349
*/
344-
public function renameFolder($old_name, $new_name) {
350+
public function renameFolder($old_name, $new_name, $expunge = true) {
345351
$this->checkConnection();
346-
return imap_renamemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name));
352+
$status = imap_renamemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name));
353+
if($expunge) $this->expunge();
354+
355+
return $status;
347356
}
348357

349358
/**
350359
* Delete Folder
351360
* @param string $name
361+
* @param boolean $expunge
352362
*
353363
* @return bool
354364
* @throws ConnectionFailedException
355365
*/
356-
public function deleteFolder($name) {
366+
public function deleteFolder($name, $expunge = true) {
357367
$this->checkConnection();
358-
return imap_deletemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
368+
$status = imap_deletemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
369+
if($expunge) $this->expunge();
370+
371+
return $status;
359372
}
360373

361374
/**

src/IMAP/Folder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public function __construct(Client $client, $folder) {
129129
* @param string $charset
130130
*
131131
* @return WhereQuery
132+
* @throws Exceptions\ConnectionFailedException
132133
*/
133134
public function query($charset = 'UTF-8'){
134135
$this->getClient()->checkConnection();
@@ -139,13 +140,15 @@ public function query($charset = 'UTF-8'){
139140

140141
/**
141142
* @inheritdoc self::query($charset = 'UTF-8')
143+
* @throws Exceptions\ConnectionFailedException
142144
*/
143145
public function search($charset = 'UTF-8'){
144146
return $this->query($charset);
145147
}
146148

147149
/**
148150
* @inheritdoc self::query($charset = 'UTF-8')
151+
* @throws Exceptions\ConnectionFailedException
149152
*/
150153
public function messages($charset = 'UTF-8'){
151154
return $this->query($charset);
@@ -359,14 +362,15 @@ protected function parseAttributes($attributes) {
359362

360363
/**
361364
* Delete the current Mailbox
365+
* @param boolean $expunge
362366
*
363367
* @return bool
364368
*
365369
* @throws Exceptions\ConnectionFailedException
366370
*/
367-
public function delete() {
371+
public function delete($expunge = true) {
368372
$status = imap_deletemailbox($this->client->getConnection(), $this->path);
369-
$this->client->expunge();
373+
if($expunge) $this->client->expunge();
370374

371375
return $status;
372376
}
@@ -375,14 +379,15 @@ public function delete() {
375379
* Move or Rename the current Mailbox
376380
*
377381
* @param string $target_mailbox
382+
* @param boolean $expunge
378383
*
379384
* @return bool
380385
*
381386
* @throws Exceptions\ConnectionFailedException
382387
*/
383-
public function move($target_mailbox) {
388+
public function move($target_mailbox, $expunge = true) {
384389
$status = imap_renamemailbox($this->client->getConnection(), $this->path, $target_mailbox);
385-
$this->client->expunge();
390+
if($expunge) $this->client->expunge();
386391

387392
return $status;
388393
}

src/IMAP/Message.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,9 @@ public function getEncoding($structure) {
721721
* Find the folder containing this message.
722722
*
723723
* @param null|Folder $folder where to start searching from (top-level inbox by default)
724+
*
724725
* @return null|Folder
726+
* @throws Exceptions\ConnectionFailedException
725727
*/
726728
public function getContainingFolder(Folder $folder = null) {
727729
$folder = $folder ?: $this->client->getFolders()->first();
@@ -758,10 +760,10 @@ public function getContainingFolder(Folder $folder = null) {
758760

759761
/**
760762
* Move the Message into an other Folder
761-
*
762-
* @param string $mailbox
763+
* @param string $mailbox
763764
*
764765
* @return bool
766+
* @throws Exceptions\ConnectionFailedException
765767
*/
766768
public function moveToFolder($mailbox = 'INBOX') {
767769
$this->client->createFolder($mailbox);
@@ -771,23 +773,30 @@ public function moveToFolder($mailbox = 'INBOX') {
771773

772774
/**
773775
* Delete the current Message
776+
* @param bool $expunge
774777
*
775778
* @return bool
779+
* @throws Exceptions\ConnectionFailedException
776780
*/
777-
public function delete() {
781+
public function delete($expunge = true) {
778782
$status = imap_delete($this->client->getConnection(), $this->uid, FT_UID);
779-
$this->client->expunge();
783+
if($expunge) $this->client->expunge();
780784

781785
return $status;
782786
}
783787

784788
/**
785789
* Restore a deleted Message
790+
* @param boolean $expunge
786791
*
787792
* @return bool
793+
* @throws Exceptions\ConnectionFailedException
788794
*/
789-
public function restore() {
790-
return imap_undelete($this->client->getConnection(), $this->uid, FT_UID);
795+
public function restore($expunge = true) {
796+
$status = imap_undelete($this->client->getConnection(), $this->uid, FT_UID);
797+
if($expunge) $this->client->expunge();
798+
799+
return $status;
791800
}
792801

793802
/**
@@ -816,7 +825,10 @@ public function hasAttachments() {
816825
*/
817826
public function setFlag($flag) {
818827
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag);
819-
return imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
828+
$status = imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
829+
$this->parseFlags();
830+
831+
return $status;
820832
}
821833

822834
/**
@@ -827,7 +839,10 @@ public function setFlag($flag) {
827839
*/
828840
public function unsetFlag($flag) {
829841
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag);
830-
return imap_clearflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
842+
$status = imap_clearflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
843+
$this->parseFlags();
844+
845+
return $status;
831846
}
832847

833848
/**

0 commit comments

Comments
 (0)