plai2010/php-msgraph 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

plai2010/php-msgraph

最新稳定版本:v1.2.3

Composer 安装命令:

composer require plai2010/php-msgraph

包简介

Microsoft Graph utilities for PHP.

README 文档

README

This is a utility package for using Microsoft Graph API in PHP applications. In this version it only supports sending email messages to the sendMail endpoint.

The use case for this package was email notification in a Laravel (10.x) application. A Laravel service provider is included.

This package is designed to work with [plai2010/php-oauth2][plai2010-oauth2] for OAuth2 token management.

Installation

The package can be installed from Packagist:

$ composer require plai2010/php-msgraph

One may also clone the source repository from Github:

$ git clone https://github.com/plai2010/php-msgraph.git
$ cd php-msgraph
$ composer install

Example: Sending Email

This package works best as a Laravel extension. It is possible to use it as just a utility library, but as one would see in this example, it can be a little awkard.

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('no-reply@example.com')
	->to('john.doe@example.com')
	->subject('hello')
	->html('<h3>Greetings!</h3>')
	->attach(file_get_contents('wave.png'), 'wave.png', 'image/png')
;
$msgraph->sendEmail($msg);

Client Manager

An application typically acts as a single MS Graph API client. MsGraphManager allows multiple clients to be configured for more complex scenarios. For example, there may a client that acts for the application, and another as agent for individual users.

// Create manager.
$manager = new PL2010\MsGraph\MsGraphManager;

// Configure a application/service level client.
$manager->configure('service', [
	'token_key' => 'msgraph:app',
]);

// Configure an agent client. Here we assume that
// the token repository recognizes the '@user' tag
// in the token key and looks up user specific
// access token.
$manager->configure('agent', [
	'token_key' => 'msgraph:agent@user',
]);

Laravel Integration

This package includes a Laravel service provider that makes a singleton MsGraphManager available two ways:

* Abstract 'msgraph' in the application container, i.e. `app('msgraph')`.
* Facade alias 'MsGraph', i.e. `MsGraph::`.

The configuration file is config/msgraph.php. It returns an associative array of client configurations by names, like this:

<?php
return [
	'msgraph_site' => [
		'token_key' => 'msgraph:app',
	],
	'msgraph_ua' => [
		'token_key' => 'msgraph:agent@user',
	],
	...
];

Also a 'msgraph' email transport is registered. So one may define a mailer in config/mailer.php like this:

	'outlook' => [
		'transport' => 'msgraph',

		// Points to entry in config/msgraph.php.
		'msgraph' => 'msgraph_site',
	]

A message can then be sent like this:

	use Illuminate\Mail\Mailable;
	$msg = (new Mailable())
		->to('john.doe@example.com')
		->subject('hello')
		->html('<h3>Greetings!</h3>')
		->send(Mail::mailer('outlook'));

统计信息

  • 总下载量: 135
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-24