php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

[Feature Request] Google OAuth Support

Open freescout-helpdesk opened this issue 5 years ago • 14 comments

Is Google OAuth authentication supported in this library? https://github.com/google/gmail-oauth2-tools/issues/18

freescout-helpdesk avatar Dec 24 '19 15:12 freescout-helpdesk

Very good point! Right now, it's not supported yet, unfortunately.

I've added a few functions in order to support OAuth, but I couldn't test it yet. It should later work like this:

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'[email protected]', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved (optional)
	'UTF-8' // Server encoding (optional)
);

try {
    $mailbox->setOAuthToken('TheOAuthAccessToken');
} catch (Exception $ex) {
    die('Authentication using OAuth failed! Error: '.$ex->getMessage());
}

try {
	$mailsIds = $mailbox->searchMailbox('ALL');
} catch(PhpImap\Exceptions\ConnectionException $ex) {
	die('IMAP connection failed: '.$ex);
}

if(!$mailsIds) {
	die('Mailbox is empty');
}

$mail = $mailbox->getMail($mailsIds[0]);

You can give it a try by using the develop branch: git checkout develop

I'll try to implement a working and tested solution, but this will earliest happen next year due to the current holidays. Any help would be appreciated.


Useful references:

  • https://developers.google.com/gmail/imap/xoauth2-protocol
  • https://github.com/google/gmail-oauth2-tools/blob/master/php/oauth2.php
  • https://stackoverflow.com/questions/2455151/sending-imap-commands-using-php
  • https://github.com/vmuthal/VivOAuthIMAP/blob/master/src/VivOAuthIMAP.php

Sebbo94BY avatar Dec 25 '19 13:12 Sebbo94BY

running automated testing for this would require changes mentioned in https://github.com/barbushin/php-imap/issues/432#issuecomment-578825685, or finding an IMAP-enabled mail provider that supports automated account creation w/ oauth imap so the account can be created & deleted on each travis build.

bapcltd-marv avatar Jan 27 '20 16:01 bapcltd-marv

Please forgive me asking, but are there any updates on this? It would be so helpful to have OAuth support for e.g. GMail/Google Workspace, and Microsoft Office 365 mail accounts.

I'm willing to help / do the implementation, what is the current status?

hrst avatar Jul 26 '21 08:07 hrst

There are no updates yet.

I've tried to work and implement it some time ago, so the current progress is available in the respective branch: 411-OAuth-Support

Feel free to submit your own pull request or add commits to the existing one.

Sebbo94BY avatar Nov 26 '21 10:11 Sebbo94BY

Hi! @freescout-helpdesk @Sebi94nbg @hrst @bapcltd-marv @rmontagud

The strategy to overcome the problem is replacing all the imap_* functions with the equivalent imap2_*

  • imap_open(...) become imap2_open(...)
  • imap_getmailboxes(...) become imap2_getmailboxes(...)

etc...

The idea behind the imap2 library is to replace the core php imap extension with a new one written in PHP. Why? We have problem with the UW-IMAP https://github.com/uw-imap/imap it seem outdate from 2019. There are many security concerns about it

I'm waiting from your feedback! I hope that imap2 is a good replacement for imap

cc @millnet-maho

francescobianco avatar May 24 '22 14:05 francescobianco

Just a heads-up to everyone: Microsoft will disable Basic Auth on Oct 1st, 2022. In contrast to Google, "App Passwords" will not be possible/allowed, only OAuth2.

Status for PHP imap_* extension (page not updated since 2020 as of writing this): https://wiki.php.net/todo/ext/imap/xoauth2

hrst avatar Jul 14 '22 09:07 hrst

When we plan to add support for oAuth to this package?

bhanu2217 avatar Aug 18 '22 01:08 bhanu2217

is there any plan to support oAuth? because Microsoft will disable Basic Auth in Januar 2023 look here: https://techcommunity.microsoft.com/t5/exchange-team-blog/basic-authentication-deprecation-in-exchange-online-september/ba-p/3609437

kurznet avatar Oct 07 '22 13:10 kurznet

Hello, as a workaround is there maybe the possibility to login using another library, like https://www.php-imap.com/ that does support oauth and then pass the object in some way to keep working with this php-imap library? We built a whole app using this library and we would do anything to avoid rewriting everything... Thanks!

nettunodev avatar Oct 15 '22 19:10 nettunodev

Hello everyone, back in 2019 I already worked on the OAuth support, but I had no chance to test it.

These changes are still on the 411-OAuth-Support branch, so if somebody could test it and provide feedback, it would be great.

Pull requests by other people are also welcome.

Since christmas time is coming soon and I'm also currently working on some other projects, I can't gurantee, that I can finish this implementation until end of December 2022. However, I'll try my best to get it done.

Sebbo94BY avatar Dec 13 '22 14:12 Sebbo94BY

has anyone be able to get this to work correctly I am trying to update for Outlook update.

VortexCallouts avatar Jan 03 '23 23:01 VortexCallouts

hi everyone, any news about OAuth-Support? we got today the "Exchange Online Basic Auth Deprecation – 7 Day Notice" in Office 365 Center

Approximately 7 days from today, we’re going to permanently disable basic authentication for the following Exchange Online protocols: MAPI, RPC, Offline Address Book, Exchange Web Services, POP, IMAP, Exchange ActiveSync and Remote PowerShell.

within the next 7 days basic auth will be deactivated, so we need a solution soon....

kurznet avatar Jan 10 '23 08:01 kurznet

@barbushin any plan to manage OAUTH?

francescobianco avatar Jan 10 '23 09:01 francescobianco

Same here

PauchardThomas avatar Jan 27 '23 13:01 PauchardThomas