和大多数人一样,微软正在关闭基本身份验证。这意味着我们需要使用现代身份验证从邮箱中检索电子邮件。
我们已经能够实现访问令牌的检索,但是,当我们建立到IMAP的连接时,连接失败,显示“NO LOGIN failed”(无登录失败)
下面是检索和连接到IMAP的代码。
/* Get the access Token */
$Secret = '**REMOVED**';
$AppID = '**REMOVED**';
$TenantID = '**REMOVED**';
$AccessToken = '';
try {
$guzzle = new \GuzzleHttp\Client(['headers' => ['User-Agent' => 'App-Token-Request']]);
$url = 'https://login.microsoftonline.com/'.$TenantID.'/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'grant_type' => 'password',
'client_id' => $AppID,
'client_secret' => $Secret,
'scope' => 'https://graph.microsoft.com/.default', //'https://outlook.office365.com/IMAP.AccessAsUser.All',// 'https://graph.microsoft.com/.default',
'username' => '**REMOVED**',
'password' => '**REMOVED**',
],
])->getBody()->getContents());
$this->info(var_dump($token));
$AccessToken = $token->access_token;
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
dd($e);
return redirect('/')->with('error', 'Error requesting access token')->with('errorDetail', json_encode($e->getResponseBody()));
}
/* Connect to IMAP */
try {
$cm = new \Webklex\PHPIMAP\ClientManager;
$client = $cm->make([
'host' => 'outlook.office365.com',
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => true,
'username' => '**REMOVED**',
'password' => $AccessToken,
'protocol' => 'imap',
'authentication' => "oath2"
]);
$client->connect();
$aFolder = $client->getFolder('FolderName');
$aMessageOBJ = $aFolder->query()->all();
$MessageCount = (clone $aMessageOBJ)->get()->count();
$this->info("Found $MessageCount to process.");
dd($client);
} catch (Exception $ex){
dd($ex);
}
获取Auth令牌有效,但连接IMAP失败。上述代码的输出返回以下内容:
class stdClass#1897 (5) {
public $token_type =>
string(6) "Bearer"
public $scope =>
string(183) "profile openid email https://graph.microsoft.com/IMAP.AccessAsUser.All https://graph.microsoft.com/SMTP.Send https://graph.microsoft.com/User.Read https://graph.microsoft.com/.default"
public $expires_in =>
int(4584)
public $ext_expires_in =>
int(4584)
public $access_token =>
string(2054) "**REMOVED**"...
}
>> TAG1 LOGIN "user@domain.com" "**AUTHTOKEN**"
<< NO LOGIN failed.
>> TAG2 LOGOUT
<< BYE Microsoft Exchange Server IMAP4 server signing off.
<< OK LOGOUT completed.
In Client.php line 376:
connection setup failed
In Client.php line 373:
[Webklex\PHPIMAP\Exceptions\AuthFailedException]
关于如何让IMAP部分工作的任何建议〉我正在使用“webklex/laravel-imap”:“2.*"、
1条答案
按热度按时间e5nqia271#
将选项“身份验证”=〉“oath2”更改为“身份验证”=〉“誓言”。
这是因为Webklex\PHPIMAP\Client检查的是字符串'oauth',而不是' oauth2'。(可能建议将此作为增强功能)
身份验证失败时的响应不应该是“NO LOGIN failed”,而应该是“NO AUTHENTICATION failed”。这样,您就知道它是作为XOAUTH2请求请求的。