⭐ Give me a star, please! ⭐
May 10, 2026 · View on GitHub
Overview
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.
Features
- OAuth2 authentication support
- Gmail IMAP integration
- Outlook / Microsoft 365 support
- Lightweight API
- Compatible with native PHP IMAP workflows
- Easy integration into existing projects
Requirements
- PHP >= 7.0
- PHP IMAP extension enabled
Installation
Install via Composer:
composer require javanile/php-imap2
Or download the latest release from GitHub.
Quick Start
<?php
$mbh = imap2_open(
$server,
$username,
$token,
OP_XOAUTH2
);
if (! $mbh) {
error_log(imap2_last_error());
throw new RuntimeException(
'Unable to open the INBOX'
);
}
Gmail OAuth2
Required OAuth scope:
https://mail.google.com/
Example IMAP server:
imap.gmail.com:993/imap/ssl
Microsoft Outlook / Office365
Example IMAP server:
outlook.office365.com:993/imap/ssl
OAuth2 authentication is supported using Microsoft identity platform access tokens.
Sandbox & Examples
Gmail Demo
Outlook Demo
Coming soon.
Error Handling
$error = imap2_last_error();
if ($error) {
echo $error;
}
Contributing
Contributions, issues, and feature requests are welcome.
Feel free to open a pull request or start a discussion.
Contributors
Related Projects
References
Microsoft Outlook
IMAP & OAuth2
- https://www.atmail.com/blog/imap-commands/
- https://developers.google.com/gmail/imap/xoauth2-protocol
- https://github.com/ddeboer/imap/issues/443#issuecomment-1172158902
Support the Project
If this project helps you, consider supporting development:

