diff --git a/README.md b/README.md
index 71cd761b..16e589ab 100644
--- a/README.md
+++ b/README.md
@@ -83,7 +83,7 @@ By default, this library uses random filenames for attachments as identical file
$mailbox = new PhpImap\Mailbox(
'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
'some@gmail.com', // Username for the before configured mailbox
- '*********', // Password for the before configured username
+ '*********', // Password or token for the before configured username
__DIR__, // Directory, where attachments will be saved (optional)
'UTF-8', // Server encoding (optional)
true, // Trim leading/ending whitespaces of IMAP path (optional)
@@ -94,14 +94,9 @@ $mailbox = new PhpImap\Mailbox(
$mailbox->setConnectionArgs(
CL_EXPUNGE // expunge deleted mails upon mailbox close
| OP_SECURE // don't do non-secure authentication
+ | OP_XOAUTH2 // set OP_XOAUTH2 connection argument if supplied password is a OAuth token.
);
-try {
- $mailbox->setOAuthToken('TheOAuthAccessToken');
-} catch (Exception $ex) {
- die('Authentication using OAuth failed! Error: '.$ex->getMessage());
-}
-
try {
// Get all emails (messages)
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
diff --git a/composer.json b/composer.json
index 9b44d8e2..3c61c058 100644
--- a/composer.json
+++ b/composer.json
@@ -24,6 +24,7 @@
},
"require": {
"php": "^7.4 || ^8.0",
+ "javanile/php-imap2": "^0.1.10",
"ext-fileinfo": "*",
"ext-iconv": "*",
"ext-imap": "*",
diff --git a/psalm.baseline.xml b/psalm.baseline.xml
index 0983eba9..12cc8fb8 100644
--- a/psalm.baseline.xml
+++ b/psalm.baseline.xml
@@ -22,9 +22,7 @@
$element->text
$element->text
-
- setOAuthToken
- getOAuthToken
+
setConnectionRetry
setConnectionRetryDelay
setExpungeOnDisconnect
diff --git a/src/PhpImap/DataPartInfo.php b/src/PhpImap/DataPartInfo.php
index 68841016..55a628aa 100644
--- a/src/PhpImap/DataPartInfo.php
+++ b/src/PhpImap/DataPartInfo.php
@@ -92,10 +92,10 @@ public function decodeAfterFetch(string $data): string
{
switch ($this->encoding) {
case ENC8BIT:
- $this->data = \imap_utf8((string) $data);
+ $this->data = \imap2_utf8((string) $data);
break;
case ENCBINARY:
- $this->data = \imap_binary((string) $data);
+ $this->data = \imap2_binary((string) $data);
break;
case ENCBASE64:
$this->data = \base64_decode((string) $data, false);
diff --git a/src/PhpImap/Imap.php b/src/PhpImap/Imap.php
index 655c1dc8..bfaa2ab0 100644
--- a/src/PhpImap/Imap.php
+++ b/src/PhpImap/Imap.php
@@ -82,12 +82,12 @@ public static function append(
string $options = null,
string $internal_date = null
): bool {
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
$imap_stream = self::EnsureConnection($imap_stream, __METHOD__, 1);
if (null !== $options && null !== $internal_date) {
- $result = \imap_append(
+ $result = \imap2_append(
$imap_stream,
$mailbox,
$message,
@@ -95,13 +95,13 @@ public static function append(
$internal_date
);
} elseif (null !== $options) {
- $result = \imap_append($imap_stream, $mailbox, $message, $options);
+ $result = \imap2_append($imap_stream, $mailbox, $message, $options);
} else {
- $result = \imap_append($imap_stream, $mailbox, $message);
+ $result = \imap2_append($imap_stream, $mailbox, $message);
}
if (false === $result) {
- throw new UnexpectedValueException('Could not append message to mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_append'));
+ throw new UnexpectedValueException('Could not append message to mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_append'));
}
return $result;
@@ -115,16 +115,16 @@ public static function body(
int $msg_number,
int $options = 0
): string {
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_body(
+ $result = \imap2_body(
self::EnsureConnection($imap_stream, __METHOD__, 1),
$msg_number,
$options
);
if (false === $result) {
- throw new UnexpectedValueException('Could not fetch message body from mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_body'));
+ throw new UnexpectedValueException('Could not fetch message body from mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_body'));
}
return $result;
@@ -135,12 +135,12 @@ public static function body(
*/
public static function check($imap_stream): object
{
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_check(self::EnsureConnection($imap_stream, __METHOD__, 1));
+ $result = \imap2_check(self::EnsureConnection($imap_stream, __METHOD__, 1));
if (false === $result) {
- throw new UnexpectedValueException('Could not check imap mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_check'));
+ throw new UnexpectedValueException('Could not check imap mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_check'));
}
/** @var object */
@@ -159,9 +159,9 @@ public static function clearflag_full(
string $flag,
int $options = 0
): bool {
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_clearflag_full(
+ $result = \imap2_clearflag_full(
self::EnsureConnection($imap_stream, __METHOD__, 1),
self::encodeStringToUtf7Imap(static::EnsureRange(
$sequence,
@@ -174,7 +174,7 @@ public static function clearflag_full(
);
if (!$result) {
- throw new UnexpectedValueException('Could not clear flag on messages!', 0, self::HandleErrors(\imap_errors(), 'imap_clearflag_full'));
+ throw new UnexpectedValueException('Could not clear flag on messages!', 0, self::HandleErrors(\imap2_errors(), 'imap_clearflag_full'));
}
return $result;
@@ -189,12 +189,12 @@ public static function clearflag_full(
*/
public static function close($imap_stream, int $flag = 0): bool
{
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
/** @var int */
$flag = $flag;
- $result = \imap_close(self::EnsureConnection($imap_stream, __METHOD__, 1), $flag);
+ $result = \imap2_close(self::EnsureConnection($imap_stream, __METHOD__, 1), $flag);
if (false === $result) {
$message = 'Could not close imap connection';
@@ -204,7 +204,7 @@ public static function close($imap_stream, int $flag = 0): bool
}
$message .= '!';
- throw new UnexpectedValueException($message, 0, self::HandleErrors(\imap_errors(), 'imap_close'));
+ throw new UnexpectedValueException($message, 0, self::HandleErrors(\imap2_errors(), 'imap_close'));
}
return $result;
@@ -217,15 +217,15 @@ public static function close($imap_stream, int $flag = 0): bool
*/
public static function createmailbox($imap_stream, string $mailbox): bool
{
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_createmailbox(
+ $result = \imap2_createmailbox(
self::EnsureConnection($imap_stream, __METHOD__, 1),
static::encodeStringToUtf7Imap($mailbox)
);
if (false === $result) {
- throw new UnexpectedValueException('Could not create mailbox!', 0, self::HandleErrors(\imap_errors(), 'createmailbox'));
+ throw new UnexpectedValueException('Could not create mailbox!', 0, self::HandleErrors(\imap2_errors(), 'createmailbox'));
}
return $result;
@@ -253,16 +253,16 @@ public static function delete(
1
));
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_delete(
+ $result = \imap2_delete(
self::EnsureConnection($imap_stream, __METHOD__, 1),
$msg_number,
$options
);
if (false === $result) {
- throw new UnexpectedValueException('Could not delete message from mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_delete'));
+ throw new UnexpectedValueException('Could not delete message from mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_delete'));
}
return $result;
@@ -275,15 +275,15 @@ public static function delete(
*/
public static function deletemailbox($imap_stream, string $mailbox): bool
{
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_deletemailbox(
+ $result = \imap2_deletemailbox(
self::EnsureConnection($imap_stream, __METHOD__, 1),
static::encodeStringToUtf7Imap($mailbox)
);
if (false === $result) {
- throw new UnexpectedValueException('Could not delete mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_deletemailbox'));
+ throw new UnexpectedValueException('Could not delete mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_deletemailbox'));
}
return $result;
@@ -296,14 +296,14 @@ public static function deletemailbox($imap_stream, string $mailbox): bool
*/
public static function expunge($imap_stream): bool
{
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_expunge(
+ $result = \imap2_expunge(
self::EnsureConnection($imap_stream, __METHOD__, 1)
);
if (false === $result) {
- throw new UnexpectedValueException('Could not expunge messages from mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_expunge'));
+ throw new UnexpectedValueException('Could not expunge messages from mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_expunge'));
}
return $result;
@@ -322,9 +322,9 @@ public static function fetch_overview(
$sequence,
int $options = 0
): array {
- \imap_errors(); // flush errors
+ \imap2_errors(); // flush errors
- $result = \imap_fetch_overview(
+ $result = \imap2_fetch_overview(
self::EnsureConnection($imap_stream, __METHOD__, 1),
self::encodeStringToUtf7Imap(self::EnsureRange(
$sequence,
@@ -336,7 +336,7 @@ public static function fetch_overview(
);
if (false === $result) {
- throw new UnexpectedValueException('Could not fetch overview for message from mailbox!', 0, self::HandleErrors(\imap_errors(), 'imap_fetch_overview'));
+ throw new UnexpectedValueException('Could not fetch overview for message from mailbox!', 0, self::HandleErrors(\imap2_errors(), 'imap_fetch_overview'));
}
/** @psalm-var list