Phpmailer oauth2 office365

PHPMailer with OAuth2 for Office365

In order to use PHPMailer with OAuth2 authentication for Office365, you will need to follow a few steps:

Step 1: Register an application in Azure Active Directory

1. Go to the Azure portal and login with your Office365 account.

2. Navigate to Azure Active Directory -> App registrations.

3. Click “New registration” and fill in the necessary details.

4. After the registration is complete, note down the “Application (client) ID” and the “Directory (tenant) ID”.

5. Under “Certificates & secrets”, create a new client secret and note down its value.

Step 2: Install PHPMailer and Microsoft Authentication Library (MSAL)

1. Open your terminal or command prompt and navigate to your PHP project directory.

2. Run the following commands to install PHPMailer and MSAL:

composer require phpmailer/phpmailer
composer require microsoft/microsoft-authentication-library-for-php

Step 3: Configure PHPMailer with OAuth2

1. Create a new PHP file and include the PHPMailer and MSAL libraries:

require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
use Microsoft\Graph\Connect\Constants;
use Microsoft\Graph\Connect\AuthenticationConfig;
use Microsoft\Graph\Connect\AuthenticationHelper;

2. Set up the OAuth2 parameters:

$clientId = 'YOUR_APP_CLIENT_ID';
$clientSecret = 'YOUR_APP_CLIENT_SECRET';
$tenantId = 'YOUR_DIRECTORY_TENANT_ID';
$redirectUri = 'https://your-callback-url';

3. Create a new instance of PHPMailer:

$mail = new PHPMailer(true);

4. Set up the SMTP configuration:

$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

5. Configure the OAuth2 provider and tokens:

$oauthConfig = AuthenticationConfig::getDefaultConfig();
$oauthConfig->setClientId($clientId);
$oauthConfig->setClientSecret($clientSecret);
$oauthConfig->setTenantNameOrId($tenantId);
$oauthConfig->setRedirectUrl($redirectUri);

6. Create a new instance of the OAuth2 provider and initialize it:

$provider = new OAuth($oauthConfig, new AuthenticationHelper());
$provider->addScope(Constants::SCOPES);
$provider->addTenant($tenantId);
$provider->setRedirectUri($redirectUri);
$accessToken = $provider->acquireToken();

7. Set the OAuth2 provider and access token in PHPMailer:

$mail->setOAuth($provider);
$mail->setAccessToken($accessToken->getToken());

8. Set the email details and send the email:

$mail->setFrom('from@example.com', 'Sender Name');
$mail->addAddress('to@example.com', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email.';
$mail->send();

Step 4: Test the setup

Save the PHP file and run it in your web server. If everything is configured correctly, the email should be sent successfully.

Make sure to replace ‘YOUR_APP_CLIENT_ID’, ‘YOUR_APP_CLIENT_SECRET’, and ‘YOUR_DIRECTORY_TENANT_ID’ with the actual values obtained from Azure Active Directory.

Also, update the ‘from@example.com’, ‘to@example.com’, ‘Sender Name’, and ‘Recipient Name’ with the appropriate email addresses and names.

Feel free to modify the email subject and body as per your requirement.

Leave a comment