-
Microsoft 365 mail with PHP
To read and send emails using Office 365 with PHP, you'll typically use the Microsoft Graph API. Here’s a step-by-step guide to get you started:
Prerequisites
- Office 365 Account: Ensure you have an Office 365 account.
- Azure App Registration: Register an application in Azure AD to get the Client ID, Tenant ID, and Client Secret.
- PHP Environment: Ensure you have a PHP environment set up.
Steps
1. Register an Application in Azure AD
- Go to the Azure Portal.
- Navigate to "Azure Active Directory" > "App registrations" > "New registration".
- Fill in the required details and register the app.
- Note the Application (client) ID and Directory (tenant) ID.
- Create a Client Secret in "Certificates & secrets" and note it.
2. Set Required API Permissions
- In the registered app, navigate to "API permissions" > "Add a permission".
- Select "Microsoft Graph".
- Add delegated permissions for
Mail.Read
,Mail.ReadWrite
,Mail.Send
.
3. Install Microsoft Graph PHP SDK
Install the Microsoft Graph SDK for PHP via Composer:
composer require microsoft/microsoft-graph
4. Authenticate and Get Access Token
Use the OAuth2 authentication flow to get an access token. Here’s an example using the
league/oauth2-client
package:composer require league/oauth2-client
Here’s a PHP script to authenticate and get the access token:
<?php require 'vendor/autoload.php'; use League\OAuth2\Client\Provider\GenericProvider; $provider = new GenericProvider([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'YOUR_REDIRECT_URI', 'urlAuthorize' => 'https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/authorize', 'urlAccessToken' => 'https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token', 'urlResourceOwnerDetails' => '', 'scopes' => 'https://graph.microsoft.com/.default' ]); // Get the authorization URL $authorizationUrl = $provider->getAuthorizationUrl(); // Get the state generated for you to store it for later $state = $provider->getState(); // Redirect to authorization URL header('Location: ' . $authorizationUrl); // Get access token if (isset($_GET['code'])) { try { // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); echo 'Access Token: ' . $accessToken->getToken(); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { exit($e->getMessage()); } } ?>
5. Use Microsoft Graph SDK to Read and Send Emails
Here's an example script to read and send emails:
<?php require 'vendor/autoload.php'; use Microsoft\Graph\Graph; use Microsoft\Graph\Model; $accessToken = 'YOUR_ACCESS_TOKEN'; $graph = new Graph(); $graph->setAccessToken($accessToken); // Reading emails $messages = $graph->createRequest("GET", "/me/mailFolders/inbox/messages") ->setReturnType(Model\Message::class) ->execute(); foreach ($messages as $message) { echo $message->getSubject() . "\n"; } // Sending an email $sendMailBody = [ "message" => [ "subject" => "Meet for lunch?", "body" => [ "contentType" => "Text", "content" => "The new cafeteria is open." ], "toRecipients" => [ [ "emailAddress" => [ "address" => "example@example.com" ] ] ] ] ]; $graph->createRequest("POST", "/me/sendMail") ->attachBody($sendMailBody) ->execute(); echo "Mail sent!"; ?>
Summary
- Register an application in Azure AD to get the Client ID, Tenant ID, and Client Secret.
- Set the necessary API permissions for Microsoft Graph.
- Authenticate using OAuth2 and obtain an access token.
- Use Microsoft Graph SDK to interact with Office 365 emails.
Ensure you replace placeholders like
YOUR_CLIENT_ID
,YOUR_CLIENT_SECRET
,YOUR_TENANT_ID
, andYOUR_ACCESS_TOKEN
with your actual credentials and tokens.
-
BLACK+DECKER BMT154C Hand Tool Kit for Home & DIY Use (154-Piece) – Includes Screwdriver, Wrench, Ratchet, Utility Knife, Saw, Claw Hammer, Measuring Tape and Plier, 6 Month Warranty, ORANGE & BLACK
Price: [price_with_discount](as of [price_update_date] – Details)
Water Pump Pliers Torpedo Level Assortment Bits with CRV Bit Driver, Two Tone Handle Screwdriver
Ratchet – 1/4 Sockets(5mm to 13mm). This ratchet helps in fast tightening & nut driving
This screw driver set is very useful to fasten or un-fasten the screw in electronic gadget
Ratchet type screwdriver it is used for screw tightening or loosening of smaller sized screws
12oz Claw Hammer made of carbon steel. Hex Keys 8 hex key with size varying from 1.5 to 6mm
250mm water pump plier & 160mm combination pliers made of carbon steel for durability[ad_2]
-
BLACK+DECKER BMT108C Hand Tool Kit for Home & DIY Use (108-Piece) – Includes Screwdriver, Wrench, Ratchet, Utility Knife, Saw, Claw Hammer, Measuring Tape and Plier, 6 Month Warranty, ORANGE & BLACK
Price: [price_with_discount](as of [price_update_date] – Details)
[ad_1]
Adjustable Wrench, Long Nose Pliers, Combination Pliers, LED Flashlight, Snap-Off Utility Knife,
Include Screwdriver 22 bits with 6 different socket. It is used for screw tightening or loosening.
18mm snap-off knife useful for cutting paper, soft plastic, small rope and thread.Adjustable Wrench
Sharp claw for easy nail removal. Strong head and a soft grip for long time usage.
3m Measuring Tape-It has lock on feature for easy use. Mini Hacksaw – Sharp blade with strong body.
8 Hex key with size varying from 1.5 to 6mm. Used for tightening and loosening screws
[ad_2]