1. Go to this page and download the library: Download plai2010/php-msgraph library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
plai2010 / php-msgraph example snippets
$ composer
$ git clone https://github.com/plai2010/php-msgraph.git
$ cd php-msgraph
$ composer install
use PL2010\MsGraph\MsGraphClient;
use PL2010\OAuth2\Contracts\TokenRepository;
// Some PL2010\OAuth2\Contracts\TokenRepository.
// Here we have a single hard coded access token
// wrapped as a token repository.
$tkrepo = new class implements TokenRepository {
public function getOAuth2Token(string $key, int $valid=0): array {
return [
// One single hard coded token regardless of $key.
'access_token' => '....',
];
}
public function putOAuth2Token(string $key, array $token): static {
return $this;
}
};
$msgraph = new MsGraphClient('sendmail', [
// OAuth2 access token will be retrieved from the
// token repository using this key.
'token_key' => 'does-not-matter',
], [
// Provide token repository.
'token_repo' => $tkrepo,
]);
$msg = (new \Symfony\Component\Mime\Email())
->from('[email protected]')
->to('[email protected]')
->subject('hello')
->html('<h3>Greetings!</h3>')
->attach(file_get_contents('wave.png'), 'wave.png', 'image/png')
;
$msgraph->sendEmail($msg);