Skip to content

PHP wrapper for easy implementation of Microsoft Graph services

License

Notifications You must be signed in to change notification settings

andrewsauder/microsoftServices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microsoft Services

PHP wrapper for easy implementation of Microsoft Graph services

Requirement

Version >=1.2 requires PHP >=8.1

Installation

composer require andrewsauder/microsoft-services

Service Configuration

$config = new \andrewsauder\microsoftServices\config();
$config->clientId = '{Azure Application ID}';
$config->clientSecret = '{Azure Client Secret}';  //certificates not yet supported
$config->tenant = 'example.com';
$config->driveId = '';                            //required if using the files service - cay be found using Graph explorer
$config->fromAddress = '[email protected]';     //required if using mail service - this is just a default

Files Usage

Get List of Files

From Root Directory

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$files = $microsoftFiles->list();

From Subdirectory

//example subdirectory: {root}/2021-0001/Building 1/Inspections
$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$files = $microsoftFiles->list( [ '2021-0001', 'Building 1', 'Inspections' ] );

Upload File

Into Root Directory

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$uploadFileResponse = $microsoftFiles->upload( 'C:\tmp\testFile.txt', 'testFile.txt' );

Into Subdirectory

//example subdirectory: {root}/2021-0001/Building 1/Inspections
$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$uploadFileResponse = $microsoftFiles->upload( 'C:\tmp\testFile.txt', 'testFile.txt', [ '2021-0001', 'Building 1', 'Inspections' ] );

Delete File

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$deleteResponse = $microsoftFiles->delete( $itemId );

Mail Usage

If no user token is provided, the application token will be used.

If the application token is being used, verify that the Azure application has correct Mail.X permissions for the email address being used. To limit application access to only certain mailboxes, use ExchangeOnline Powershell to apply access policy. More info https://learn.microsoft.com/en-us/powershell/module/exchange/new-applicationaccesspolicy?view=exchange-ps

If a user access token is provided when creating the service (mail($config, 'user-access-token-string')), verify that the user has 'send on behalf' of or 'send as' permissions configured properly in Office 365.

Send Email

If the from address is not provided, the default from address in the config will be used.

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$microsoftMail->addAttachment( 'C:\tmp\testFile.txt' );
$rsp = $microsoftMail->send( '[email protected]', 'Subject', 'HTML compatible message', '[email protected]' );

if( $rsp->getStatus() < 200 || $rsp->getStatus() >= 300 ) {
    error_log( 'Failed' );
}

Get All Messages

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$messages = $microsoftMail->getAllMessages( '[email protected]' );

Get All Messages from Specific Folder

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$messages = $microsoftMail->getMessagesInFolder( '[email protected]', 'mail-folder-id' );

Get All Folders

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$folders = $microsoftMail->getFolders( '[email protected]' );

Get Attachments for Message

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$attachments = $microsoftMail->getAttachments( '[email protected]', 'message-id' );

Delete Message

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$graphResponse = $microsoftMail->deleteMessage( '[email protected]', 'message-id' );

User Usage

Get All Users in Organization

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->allUsersInOrganization();

Get User by User Principal Name

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUserByUserPrincipalName( '[email protected]' );

Get User by Id

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUserById( '15bd6895-bf60-4125-a1d2-affb7e0de5d8' );

Get Users By Advanced Filter

See https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http for filter details

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUsersByFilter( 'startswith(userPrincipalName,"andrew")' );

About

PHP wrapper for easy implementation of Microsoft Graph services

Topics

Resources

License

Stars

Watchers

Forks

Languages