msgraph-sdk-php
msgraph-sdk-php copied to clipboard
The user account which was used to submit this request does not have the right to send mail on behalf of the specified sending account., Cannot submit message.ok
I try to send un email with ClientCredential and it doesn't work, i dont known if i do ther right thing :
Here is my code :
$tokenRequestContext = new ClientCredentialContext(
'tenantId',
'clientId',
'secret'
);
$graphServiceClient = new GraphServiceClient($tokenRequestContext);
try {
$sender = new EmailAddress();
$sender->setAddress('[email protected]');
$sender->setName('John Doe');
$fromRecipient = new Recipient();
$fromRecipient->setEmailAddress($sender);
$recipients = [];
$recipientEmail = new EmailAddress();
$recipientEmail->setAddress('[email protected]');
$recipientEmail->setName('Jane Doe');
$toRecipient = new Recipient();
$toRecipient->setEmailAddress($recipientEmail);
$recipients[] = $toRecipient;
$emailBody = new ItemBody();
$emailBody->setContent('Dummy content');
$emailBody->setContentType(new BodyType(BodyType::TEXT));
$message = new Message();
$message->setSubject('Test Email');
$message->setFrom($fromRecipient);
$message->setToRecipients($recipients);
$message->setBody($emailBody);
$requestBody = new SendMailPostRequestBody();
$requestBody->setMessage($message);
/** @var UserItemRequestBuilder $user */
$user = $graphServiceClient->users()->byUserId('userPrincipalName');
$response = $user->sendMail()->post($requestBody)->wait();
dump($response);
} catch (ApiException $ex) {
echo $ex->getError()->getMessage();
}
I try to delegate right to send on userPrincipalName email but it doesn't work i read doc lot of times and i don't understand why i doesn't work I dont know if i can send an email like this with credential client account ?
Can you help me please ? Thank you in advance