Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Error connecting to Outlook since today #297

Closed
alexv96 opened this issue Oct 12, 2022 · 4 comments
Closed

Error connecting to Outlook since today #297

alexv96 opened this issue Oct 12, 2022 · 4 comments

Comments

@alexv96
Copy link

alexv96 commented Oct 12, 2022

Since today at 6 am I started to have problems with the library that no longer generated authentication connection, which I do not know how to solve it because there are key processes in my platform that captures data.

$cm = new ClientManager([
                    'options' => [
                        'fetch' => \Webklex\PHPIMAP\IMAP::FT_PEEK,
                        'fetch_order' => 'desc',
                        'sequence' => \Webklex\PHPIMAP\IMAP::ST_UID,
                    ],
                ]);

                static::$client = $cm->make([
                    'host'          => $email->server,
                    'port'          => $email->port,
                    'protocol'      => $email->server_type,
                    'encryption'    => $email->encryption,
                    'validate_cert' => true,
                    'username'      => $email->email,
                    'password'      => $email->password,
                ]);

                try {
                    static::$client->connect();
                } catch (\Throwable $th) {
                    dd($th);
                }

I currently have the following versions:
Windows 11 (testing)
Debian 11 (production)
Laravel 7
PHP 7.4
"webklex/php-imap":"^2.4
Provider Google and Outlook

image

@alexv96 alexv96 changed the title Error connecting to Google and Outlook since today Error connecting to Outlook since today Oct 12, 2022
@knightcott-paul
Copy link

You are probably affected by this: https://techcommunity.microsoft.com/t5/exchange-team-blog/basic-authentication-deprecation-in-exchange-online-september/ba-p/3609437

It should be possible to switch to Oauth authentication

@alexv96
Copy link
Author

alexv96 commented Oct 14, 2022

The question is if OAuth would let me connect through the console since the only thing I do are processes by console that captures the mail and reads and publishes them on the platform.

@knightcott-paul
Copy link

I think it does. I've just posted the steps I've been using.

@AzzaAzza69
Copy link

AzzaAzza69 commented Oct 17, 2022

sample code snippet to connect to a shared/regular mailbox on office365 via oauth, hope this helps someone:

	const IMAP_HOST='outlook.office365.com';
	const IMAP_LOGINNAME='loginname@somewhere.com';
	const IMAP_PASSWORD='?';
	const IMAP_SHARED_MAILBOX='sharedmailbox@somewhere.com';

	const OAUTH_URL='https://#.microsoftonline.com/organizations/oauth2/v2.0/token';
	const OAUTH_POSTFIELDS=[
		'client_id'=>'????????-????-????-????-????????????',
		'client_secret'=>'????????????????????????????????????????',
		'scope'=>'https://outlook.office365.com/IMAP.AccessAsUser.All',
		'grant_type'=>'password',
		'username'=>self::IMAP_LOGINNAME,
		'password'=>self::IMAP_PASSWORD
	];

	function connect(){
		$ch=curl_init();
		try{
			curl_setopt($ch,CURLOPT_URL, self::OAUTH_URL);
			curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query(self::OAUTH_POSTFIELDS));
			curl_setopt($ch,CURLOPT_POST, 1);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
			$oResult=curl_exec($ch);
			if(empty($oResult)) throw New Exception('Missing results.');
			$oResult=json_decode($oResult);
			if(empty($oResult)) throw New Exception('Error decoding json result.');
			if(!isset($oResult->access_token)) throw New Exception('Missing access token from result.');
			$sAccessToken=$oResult->access_token;
		}finally{
			curl_close($ch);
		}

		$cm=new ClientManager([]);

		// connect to shared mailbox
		$client = $cm->make([
			'protocol'=>'imap',
			'host'=>self::IMAP_HOST,
			'post'=>993,
			'encryption'=>'ssl',
			'validate_cert'=>true,
			'username'=>IMAP_SHARED_MAILBOX,
			// or if you want to connect to a regular mailbox:
			// 'username'=>self::IMAP_LOGINNAME,
			'password'=>$sAccessToken,
			'authentication'=>"oauth",
		]);
		$client->connect();

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants