It looks like you have already taken the necessary steps to authenticate the user, such as registering the app with the sys admin and adding the application permissions.
However, it is possible that the user does not have the correct permissions to send emails. You can check this by going to the Office 365 Admin Center and navigating to the Users tab. From there, you can select the user and check the permissions they have. If they do not have the correct permissions, you can add them.
It is also possible that the credentials you are using are incorrect. You can double check the credentials you are using in the config('office365mail.client_id') and config('office365mail.client_secret') variables.
If the credentials are correct and the user has the correct permissions, you can try to debug the issue further by using the Guzzle HTTP Client to log the request and response. You can do this by adding the following code to the getAccessToken() function:
$guzzle = new \GuzzleHttp\Client([
'debug' => true
]);
$guzzle->post($url, [
'form_params' => [
'client_id' => config('office365mail.client_id'),
'client_secret' => config('office365mail.client_secret'),
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
]);
echo $guzzle->getHistory()->getLastRequest()->getUri();
echo $guzzle->getHistory()->getLastResponse()->getBody();
This will log the request and response, which can help you debug the issue further.