-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_reader.php
64 lines (49 loc) · 1.9 KB
/
mail_reader.php
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
<?php
namespace O365Integration;
class O365MailReader {
private $accessToken;
public function __construct($accessToken) {
$this->accessToken = $accessToken;
}
public function getInboxMessages($limit = 20) {
// Microsoft Graph API endpoint'i
$graph_url = 'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages';
// Query parametreleri
$params = [
'$top' => $limit,
'$orderby' => 'receivedDateTime DESC',
'$select' => 'subject,from,receivedDateTime,bodyPreview'
];
$url = $graph_url . '?' . http_build_query($params);
$headers = [
'Authorization: Bearer ' . $this->accessToken,
'Content-Type: application/json',
'Prefer: outlook.body-content-type="text"'
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
throw new \Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
if ($httpCode === 401) {
throw new \Exception('Başarılı bir giriş yaptınız, gerekli izin olmadığı için mail okuyamazsınız');
}
if ($httpCode !== 200) {
throw new \Exception('API hatası: HTTP ' . $httpCode);
}
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('JSON decode hatası: ' . json_last_error_msg());
}
return $data;
}
}