diff --git a/src/Message.php b/src/Message.php index 9d15a7b8..dc610c5b 100755 --- a/src/Message.php +++ b/src/Message.php @@ -1176,7 +1176,7 @@ public function restore(bool $expunge = true): bool { */ public function setFlag(array|string $flag): bool { $this->client->openFolder($this->folder_path); - $flag = "\\" . trim(is_array($flag) ? implode(" \\", $flag) : $flag); + $flag = self::sanitizeFlags($flag); $sequence_id = $this->getSequenceId(); try { $status = $this->client->getConnection()->store([$flag], $sequence_id, $sequence_id, "+", true, $this->sequence)->validatedData(); @@ -1207,7 +1207,7 @@ public function setFlag(array|string $flag): bool { public function unsetFlag(array|string $flag): bool { $this->client->openFolder($this->folder_path); - $flag = "\\" . trim(is_array($flag) ? implode(" \\", $flag) : $flag); + $flag = self::sanitizeFlags($flag); $sequence_id = $this->getSequenceId(); try { $status = $this->client->getConnection()->store([$flag], $sequence_id, $sequence_id, "-", true, $this->sequence)->validatedData(); @@ -1257,6 +1257,19 @@ public function removeFlag(array|string $flag): bool { return $this->unsetFlag($flag); } + /** + * Format the flag string. + * Allows keywords like $MDNSent or $Forwarded + * @param array|string $flag + * + * @return string + */ + public static function sanitizeFlags(array|string $flag): string { + return trim(is_array($flag) + ? (implode(" ", array_map(fn ($flag) => str_starts_with($flag, '$') ? $flag : "\\$flag", $flag))) + : (str_starts_with($flag, '$') ? $flag : "\\$flag")); + } + /** * Get all message attachments. * diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 0ce55cd0..24db0e44 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -263,6 +263,12 @@ public function testIssue348() { self::assertSame("application/pdf", $attachment->getMimeType()); } + public function testSanitizeFlag(): void { + self::assertEquals('\\Seen', Message::sanitizeFlags('Seen')); + self::assertEquals('$Forwarded', Message::sanitizeFlags('$Forwarded')); + self::assertEquals('\\Seen $MDNSent $Junk \\Flagged', Message::sanitizeFlags(['Seen', '$MDNSent', '$Junk', 'Flagged'])); + } + /** * Create a new protocol mockup *