/var/www/html_fr/wp-content/plugins/fluent-smtp/app/Services/Mailer/Providers/Outlook/API.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace FluentMail\App\Services\Mailer\Providers\Outlook;

use 
FluentMail\Includes\Support\Arr;

class 
API
{
    private 
$clientId;
    private 
$clientSecret;

    public function 
__construct($clientId ''$clientSecret '')
    {
        
$this->clientId     $clientId;
        
$this->clientSecret $clientSecret;
    }

    public function 
getAuthUrl()
    {

        
$fluentClient = new \FluentMail\Includes\OAuth2Provider($this->getConfig());

        return 
$fluentClient->getAuthorizationUrl();

    }

    public function 
generateToken($authCode)
    {
        return 
$this->sendTokenRequest('authorization_code', [
            
'code' => $authCode
        
]);
    }

    
/**
     * @return mixed|string
     */
    
public function sendTokenRequest($type$params)
    {
        
$fluentClient = new \FluentMail\Includes\OAuth2Provider($this->getConfig());
        try {
            
$tokens $fluentClient->getAccessToken($type$params);
            return 
$tokens;
        } catch (
\Exception$exception) {
            return new 
\WP_Error(422$exception->getMessage());
        }
    }

    
/**
     * @return array | \WP_Error
     */
    
public function sendMime($mime$accessToken)
    {
        
$response wp_remote_request('https://graph.microsoft.com/v1.0/me/sendMail', [
            
'method'  => 'POST',
            
'headers' => [
                
'Authorization' => 'Bearer ' $accessToken,
                
'Content-Type'  => 'text/plain'
            
],
            
'body'    => $mime
        
]);

        if (
is_wp_error($response)) {
            return 
$response;
        }

        
$responseCode wp_remote_retrieve_response_code($response);

        if (
$responseCode >= 300) {
            
$error Arr::get($response'response.message');

            if (!
$error) {
                
$responseBody json_decode(wp_remote_retrieve_body($response), true);

                
$error Arr::get($responseBody'error.message');
                if (!
$error) {
                    
$error __('Something with wrong with Outlook API. Please check your API Settings''fluent-smtp');
                }
            }

            return new 
\WP_Error($responseCode$error);
        }

        
$header wp_remote_retrieve_headers($response);

        return 
$header->getAll();
    }

    public function 
getRedirectUrl()
    {
        return 
rest_url('fluent-smtp/outlook_callback');
    }

    private function 
getConfig()
    {
        return [
            
'clientId'                => $this->clientId,
            
'clientSecret'            => $this->clientSecret,
            
'redirectUri'             => $this->getRedirectUrl(),
            
'urlAuthorize'            => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
            
'urlAccessToken'          => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
            
'urlResourceOwnerDetails' => '',
            
'scopes'                  => 'https://graph.microsoft.com/user.read https://graph.microsoft.com/mail.readwrite https://graph.microsoft.com/mail.send https://graph.microsoft.com/mail.send.shared offline_access'
        
];
    }

}