-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.php
More file actions
144 lines (123 loc) · 4.06 KB
/
main.php
File metadata and controls
144 lines (123 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// <ProgramSnippet>
// Enable loading of Composer dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');
require_once 'GraphHelper.php';
print('PHP Graph Tutorial'.PHP_EOL.PHP_EOL);
// Load .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['CLIENT_ID', 'TENANT_ID', 'GRAPH_USER_SCOPES']);
initializeGraph();
greetUser();
$choice = -1;
while ($choice != 0) {
echo('Please choose one of the following options:'.PHP_EOL);
echo('0. Exit'.PHP_EOL);
echo('1. Display access token'.PHP_EOL);
echo('2. List my inbox'.PHP_EOL);
echo('3. Send mail'.PHP_EOL);
echo('4. Make a Graph call'.PHP_EOL);
$choice = (int)readline('');
switch ($choice) {
case 1:
displayAccessToken();
break;
case 2:
listInbox();
break;
case 3:
sendMail();
break;
case 4:
makeGraphCall();
break;
case 0:
default:
print('Goodbye...'.PHP_EOL);
}
}
// </ProgramSnippet>
// <InitializeGraphSnippet>
function initializeGraph(): void {
GraphHelper::initializeGraphForUserAuth();
}
// </InitializeGraphSnippet>
// <GreetUserSnippet>
function greetUser(): void {
try {
$user = GraphHelper::getUser();
print('Hello, '.$user->getDisplayName().'!'.PHP_EOL);
// For Work/school accounts, email is in Mail property
// Personal accounts, email is in UserPrincipalName
$email = $user->getMail();
if (empty($email)) {
$email = $user->getUserPrincipalName();
}
print('Email: '.$email.PHP_EOL.PHP_EOL);
} catch (Exception $e) {
print('Error getting user: '.$e->getMessage().PHP_EOL.PHP_EOL);
}
}
// </GreetUserSnippet>
// <DisplayAccessTokenSnippet>
function displayAccessToken(): void {
try {
$token = GraphHelper::getUserToken();
print('User token: '.$token.PHP_EOL.PHP_EOL);
} catch (Exception $e) {
print('Error getting access token: '.$e->getMessage().PHP_EOL.PHP_EOL);
}
}
// </DisplayAccessTokenSnippet>
// <ListInboxSnippet>
function listInbox(): void {
try {
$messages = GraphHelper::getInbox();
// Output each message's details
foreach ($messages->getValue() as $message) {
print('Message: '.$message->getSubject().PHP_EOL);
print(' From: '.$message->getFrom()->getEmailAddress()->getName().PHP_EOL);
$status = $message->getIsRead() ? "Read" : "Unread";
print(' Status: '.$status.PHP_EOL);
print(' Received: '.$message->getReceivedDateTime()->format(\DateTimeInterface::RFC2822).PHP_EOL);
}
$nextLink = $messages->getOdataNextLink();
$moreAvailable = isset($nextLink) && $nextLink != '' ? 'True' : 'False';
print(PHP_EOL.'More messages available? '.$moreAvailable.PHP_EOL.PHP_EOL);
} catch (Exception $e) {
print('Error getting user\'s inbox: '.$e->getMessage().PHP_EOL.PHP_EOL);
}
}
// </ListInboxSnippet>
// <SendMailSnippet>
function sendMail(): void {
try {
// Send mail to the signed-in user
// Get the user for their email address
$user = GraphHelper::getUser();
// For Work/school accounts, email is in Mail property
// Personal accounts, email is in UserPrincipalName
$email = $user->getMail();
if (empty($email)) {
$email = $user->getUserPrincipalName();
}
GraphHelper::sendMail('Testing Microsoft Graph', 'Hello world!', $email);
print(PHP_EOL.'Mail sent.'.PHP_EOL.PHP_EOL);
} catch (Exception $e) {
print('Error sending mail: '.$e->getMessage().PHP_EOL.PHP_EOL);
}
}
// </SendMailSnippet>
// <MakeGraphCallSnippet>
function makeGraphCall(): void {
try {
GraphHelper::makeGraphCall();
} catch (Exception $e) {
print(PHP_EOL.'Error making Graph call'.PHP_EOL.PHP_EOL);
}
}
// </MakeGraphCallSnippet>
?>