# Microsoft Outlook Setup (OAuth2)

## Settings in Microsoft Entra

### Application Registration

1. Sign in to the [Microsoft Entra admin center.](https://entra.microsoft.com/)

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FRT1vf7Ak3soAxf0DJWa7%2FMicrosoftEntraDeshboard.png?alt=media&#x26;token=a9d7cdc4-fca3-4777-a296-1f6f4b2d1f26" alt=""><figcaption><p>Microsoft Entra admin center home page</p></figcaption></figure>

2. Go to "**Entra ID**" -> "**App registrations**". Then click "**New registration**" to register a new application.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FICTK6bc1HBmuH2RVpt4m%2FMicrosoftEntraNewAppRegistration.png?alt=media&#x26;token=15daba0d-0492-4fd9-b69c-6de833633cdd" alt=""><figcaption><p>Registering a new application</p></figcaption></figure>

3. Select the following parameters for your application:

* **Name** - enter a name for your application.
* **Supported account types** - select "**Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant)**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FDASN2PVPj3JxBlDqbcfF%2FApplicationNameAccTypes.png?alt=media&#x26;token=9d1ab2c3-4c76-46f4-8c89-bb0e5d213eab" alt=""><figcaption><p>Application parameters</p></figcaption></figure>

4. Specify the Redirect URL:

* **Select a platform** — select "**Web**".
* **URL**:

```
https://192.168.100.71/pbxcore/api/v3/mail-settings/oauth2-callback
```

Replace 192.168.100.71 with your MikoPBX address.

Then click "**Register**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FZvx0E2kvHlxc4I1Othxt%2FMicrosoftEntraRedirectURl.png?alt=media&#x26;token=9ed0a14a-d8bc-43d0-bbcb-61400d5538b9" alt=""><figcaption><p>Redirect URL parameters</p></figcaption></figure>

5. The application will be created. Save the Client ID — you will need it in the future for configuration inside the MikoPBX web interface.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FqYFnYqerAuXbVDU4TYtZ%2FCreatedApplicationOverview.png?alt=media&#x26;token=bff22030-c88d-45e9-a96b-ef28e0fa6e57" alt=""><figcaption><p>Created application overview page</p></figcaption></figure>

### Granting Permissions and Creating a Client Secret

1. From the application's main page, go to "**Manage**" -> "**API permissions**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FYwn4TFIppN0NmTLMvSno%2FMicrosoftEntraAPIpermissions.png?alt=media&#x26;token=699c245d-ffc0-4884-a035-445ebe4bdbfe" alt=""><figcaption><p>"API permissions" section</p></figcaption></figure>

2. Click "**Add a permission**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F02HYedQZX86RY0TpxA8m%2FMicrosoftEntraAddPermission.png?alt=media&#x26;token=aaabebb4-888b-4215-afe8-1d8f0fffd9e9" alt=""><figcaption><p>Adding permissions</p></figcaption></figure>

3. In the "**Microsoft Graph**" section, select "**Delegated Permissions**". Enter "**SMTP**" in the search bar. Check the box next to "**SMTP.Send**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FJgz6GAdTpi1jpb7FfRvK%2FAPISmtp.Send.png?alt=media&#x26;token=82b9d8c7-94ac-4da5-ac91-ed6656a11966" alt=""><figcaption><p>Granting the "SMTP.Send" permission</p></figcaption></figure>

4. Also enter "**offline**" in the search bar. Check the box next to "**offline\_access**".

Click "**Add permissions**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FsZZ1TWB23zLnU93PEy9g%2FAPIoffline_access.png?alt=media&#x26;token=5addcab4-6302-4b94-8d2a-35a2ddbea259" alt=""><figcaption><p>Granting the "offline_access" permission</p></figcaption></figure>

5. Next, go to "**Certificates & secrets**" -> "**Client secrets**". Click "**New client secret**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FBg6ryzWAFkGKYHhLy1cD%2FcreatingNewClientSecret.png?alt=media&#x26;token=c7b2cc96-6ff2-4118-b318-667385915488" alt=""><figcaption><p>Creating a new Secret ID</p></figcaption></figure>

6. Set the required parameters:

* **Description** - an arbitrary description.
* **Expires** - the duration for which you are issuing this client secret. It will be needed for application authentication in MikoPBX.

{% hint style="info" %}
After expiration, the created client secret will stop functioning and you will need to repeat the process of creating a new key and connecting to MikoPBX.
{% endhint %}

{% hint style="danger" %}
After creation, the Client Secret value will be shown only once. Do not forget to copy it into the MikoPBX web interface.
{% endhint %}

Click "**Add**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2For9Lp9AaXR0Ra2dl29mJ%2FnewClientSecret.png?alt=media&#x26;token=2fec4339-8c9c-4942-ad73-12b3378c6fe3" alt=""><figcaption><p>Parameters for creating a new client secret</p></figcaption></figure>

7. Copy the "**Value**" (<mark style="color:$danger;">**not the Secret ID!**</mark>). It will be needed for configuration in the MikoPBX web interface.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2Fj1w841rfa5zu27JfEWQs%2FcopyingSecretKeyValue.png?alt=media&#x26;token=ba62d639-96a4-4ccf-8266-d3881f6420ee" alt=""><figcaption><p>Copying the Value of the previously created Client Secret</p></figcaption></figure>

#### Granting Permissions to a User

For the application to work correctly, you need to grant permission to use the SMTP protocol for the user whose mailbox you are authorizing during this setup. To do this, follow these steps:

1. Go to the organization's admin center ([link](https://admin.microsoft.com/)).

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FOAHVy544rynKkIsIlHcr%2FMicrosoftAdminCenterDashboard.png?alt=media&#x26;token=2a61862f-57d1-4b59-85ab-9f72d4418bc0" alt=""><figcaption><p>Microsoft Admin Center home page</p></figcaption></figure>

2. Go to "**Users**" -> "**Active Users**". Click on the name of the user account under which the application is being created.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2Fvcqs4A13X2Qe0AA9xFYd%2FMicrosoftAdminCenterActiveUsers.png?alt=media&#x26;token=b9c988df-7723-4418-8591-9554864505f9" alt=""><figcaption><p>"Active Users" section in Microsoft Admin Center</p></figcaption></figure>

3. In the account, go to the "**Mail**" section and select "**Manage email apps**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FiJ3bRIbKT8VBYSW6y7X2%2FMicrosoftAdminCenterUserMail.png?alt=media&#x26;token=d963b22c-a9d6-419f-ada0-8695fcbfadc0" alt=""><figcaption><p>"Mail" section in the user account</p></figcaption></figure>

4. Make sure that "**Authenticated SMTP**" is allowed. Save the changes by clicking "**Save changes**".

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FVOOHchqkzwFxLiockBQg%2FAllowAuthSMTP.png?alt=media&#x26;token=629f8ad2-954a-4d63-8c47-97df993af791" alt=""><figcaption><p>Allowing Authenticated SMTP for the selected user</p></figcaption></figure>

### Settings in MikoPBX

1. Go to the MikoPBX web interface. Then "**System**" -> "**Mail and Notifications**" -> "**SMTP Settings**".

Fill in all the required fields:

* **Sender address, Sender name** — your email and the name from which the emails will be sent.
* **Authentication type** — OAuth2.
* **SMTP login** — your email.
* **OAuth2 Provider** — Microsoft/Outlook.
* **Application ID (Client ID), Secret key (Client Secret)** — data from Microsoft Entra.

Leave all other settings at their default values. A more detailed description can be found in the main article about mail parameters ([link](https://docs.mikopbx.com/mikopbx/manual/system/mail-settings-1)).

After that, click "**Save**"!

2. Click "**Connect via OAuth2**". Sign in to your Microsoft account. Then confirm granting all requested permissions.

Upon successful authorization, you will see the corresponding window.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FHiBjrrzCehI4ftJxqtlB%2FsuccessfulOAuth2.png?alt=media&#x26;token=69145b4a-6765-4e14-a492-39c850a0ae58" alt="" width="375"><figcaption><p>Successful authorization</p></figcaption></figure>
