php-imap2 provides a lightweight and modern interface for working with IMAP mailboxes in PHP, including OAuth2 authentication support for providers like Gmail and Outlook.
Designed as a simple drop-in alternative for projects that need secure IMAP access without legacy authentication.
- OAuth2 authentication support
- Gmail IMAP integration
- Outlook / Microsoft 365 support
- Lightweight API
- Compatible with native PHP IMAP workflows
- Easy integration into existing projects
- PHP >= 7.0
- PHP IMAP extension enabled
Install via Composer:
composer require javanile/php-imap2Or download the latest release from GitHub.
<?php
$mbh = imap2_open(
$server,
$username,
$token,
OP_XOAUTH2
);
if (! $mbh) {
error_log(imap2_last_error());
throw new RuntimeException(
'Unable to open the INBOX'
);
}Required OAuth scope:
https://mail.google.com/
Example IMAP server:
imap.gmail.com:993/imap/ssl
Example IMAP server:
outlook.office365.com:993/imap/ssl
OAuth2 authentication is supported using Microsoft identity platform access tokens.
Coming soon.
$error = imap2_last_error();
if ($error) {
echo $error;
}Contributions, issues, and feature requests are welcome.
Feel free to open a pull request or start a discussion.
- https://www.atmail.com/blog/imap-commands/
- https://developers.google.com/gmail/imap/xoauth2-protocol
- ddeboer/imap#443 (comment)
If this project helps you, consider supporting development:


