Skip to content

Commit ac62c52

Browse files
author
David Carr
committed
removed unused files and added 2 traits emails and contacts
1 parent 8197889 commit ac62c52

9 files changed

+244
-80
lines changed

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ All notable changes to `MsGraph` will be documented in this file.
66

77
### Added
88
- Everything
9+
10+
## Version 1.0.1
11+
- added 2 traits
12+
- Emails - methods for listing emails and attachments and sending, replying and forwarding
13+
- Contacts - List all contacts

contributing.md

-23
This file was deleted.

readme.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ Please see the [changelog](changelog.md) for more information on what has change
4747

4848
## Contributing
4949

50-
Please see [contributing.md](contributing.md) for details and a todolist.
50+
Contributions are welcome and will be fully credited.
51+
52+
Contributions are accepted via Pull Requests on [Github](https://github.com/daveismynamelaravel/msgrapth).
53+
54+
## Pull Requests
55+
56+
- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.
57+
58+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
59+
60+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
5161

5262
## Security
5363

54-
If you discover any security related issues, please email author email instead of using the issue tracker.
64+
If you discover any security related issues, please email dave@daveismyname.com email instead of using the issue tracker.
5565

5666
## Credits
5767

src/Api/Contacts.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
trait Contacts {
66

7-
public function contacts($limit = 25, $offset = 0, $skip = 0)
7+
public function contacts($limit = 25, $offset = 50, $skip = 0)
88
{
9-
//$limit = 25;
10-
//$offset = 50;
119
$skip = request('next', $skip);
1210

1311
$messageQueryParams = array (

src/Api/Emails.php

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
namespace DaveismynameLaravel\MsGraph\Api;
4+
5+
trait Emails {
6+
7+
public function emails($limit = 25, $skip = 0, $folderId = null)
8+
{
9+
$messageQueryParams = array (
10+
"\$skip" => $skip,
11+
"\$top" => $limit,
12+
"\$count" => "true",
13+
);
14+
15+
if ($folderId == null) {
16+
$folder = 'Inbox';
17+
} else {
18+
$folder = $folderId;
19+
}
20+
21+
//get inbox from folders list
22+
$folder = self::get("me/mailFolders?\$filter=startswith(displayName,'$folder')");
23+
24+
//folder id
25+
$inbox = $folder['value'][0]['id'];
26+
27+
//get messages from inbox folder
28+
$emails = self::get("me/mailFolders/$inbox/messages?".http_build_query($messageQueryParams));
29+
30+
$total = $emails['@odata.count'];
31+
$previous = null;
32+
$next = null;
33+
if (isset($emails['@odata.nextLink'])) {
34+
$first = explode('$skip=', $emails['@odata.nextLink']);
35+
36+
$skip = explode('&', $first[1]);
37+
$previous = $skip[0]-$limit;
38+
$next = $skip[0];
39+
40+
if ($previous < 0) {
41+
$previous = 0;
42+
}
43+
44+
if ($next == $total) {
45+
$next = null;
46+
}
47+
}
48+
49+
return [
50+
'emails' => $emails,
51+
'total' => $total,
52+
'previous' => $previous,
53+
'next' => $next
54+
];
55+
}
56+
57+
public function emailAttachments($email_id)
58+
{
59+
return self::get("me/messages/".$email_id."/attachments");
60+
}
61+
62+
public function emailInlineAttachments($email)
63+
{
64+
$attachments = self::emailAttachments($email['id']);
65+
66+
//replace every case of <img='cid:' with the base64 image
67+
$email['body']['content'] = preg_replace_callback(
68+
'~cid.*?"~',
69+
function($m) use($attachments) {
70+
71+
//remove the last quote
72+
$parts = explode('"',$m[0]);
73+
74+
//remove cid:
75+
$contentId = str_replace('cid:', '', $parts[0]);
76+
77+
//loop over the attachments
78+
foreach ($attachments['value'] as $file) {
79+
//if there is a match
80+
if ($file['contentId'] == $contentId) {
81+
//return a base64 image with a quote
82+
return "data:".$file['contentType'].";base64,".$file['contentBytes'].'"';
83+
}
84+
}
85+
},
86+
$email['body']['content']
87+
);
88+
89+
return $email;
90+
}
91+
92+
public function emailSend($subject, $message, $to, $cc, $bcc, $attachments = null)
93+
{
94+
//send an email to a draft
95+
$draft = self::post('me/messages', self::emailPrepare($subject, $message, $to, $cc, $bcc));
96+
97+
if ($attachments != null) {
98+
foreach($attachments as $file) {
99+
//create an attachment and send to the draft message based on the message id
100+
$attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file);
101+
}
102+
}
103+
104+
//send the draft message now it's complete
105+
return self::post('me/messages/'.$draft['id'].'/send', []);
106+
}
107+
108+
public function emailSendReply($id, $message, $to, $cc, $bcc, $attachments = null)
109+
{
110+
//send an email to a draft
111+
$draft = self::post("me/messages/$id/createReplyAll", self::prepareReply($message, $to, $cc, $bcc));
112+
113+
if ($attachments != null) {
114+
foreach($attachments as $file) {
115+
//create an attachment and send to the draft message based on the message id
116+
$attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file);
117+
}
118+
}
119+
120+
//send the draft message now it's complete
121+
self::post('me/messages/'.$draft['id'].'/send', []);
122+
}
123+
124+
public function emailSendForward($id, $message, $to, $cc, $bcc, $attachments = null)
125+
{
126+
//send an email to a draft
127+
$draft = self::post("me/messages/$id/createForward", self::emailPrepareReply($message, $to, $cc, $bcc));
128+
129+
if ($attachments != null) {
130+
foreach($attachments as $file) {
131+
//create an attachment and send to the draft message based on the message id
132+
$attachment = self::post('me/messages/'.$draft['id'].'/attachments', $file);
133+
}
134+
}
135+
136+
//send the draft message now it's complete
137+
self::post('me/messages/'.$draft['id'].'/send', []);
138+
}
139+
140+
protected static function emailPrepare($subject, $message, $to, $cc = null, $bcc = null)
141+
{
142+
143+
$parts = explode(',', $to);
144+
$toArray = [];
145+
foreach($parts as $to) {
146+
$toArray[]["emailAddress"] = ["address" => $to];
147+
}
148+
149+
$ccArray = [];
150+
if ($cc != null) {
151+
$parts = explode(',', $cc);
152+
foreach($parts as $cc) {
153+
$ccArray[]["emailAddress"] = ["address" => $cc];
154+
}
155+
}
156+
157+
$bccArray = [];
158+
if ($bcc != null) {
159+
$parts = explode(',', $bcc);
160+
foreach($parts as $bcc) {
161+
$bccArray[]["emailAddress"] = ["address" => $bcc];
162+
}
163+
}
164+
165+
return [
166+
"subject" => $subject,
167+
"body" => [
168+
"contentType" => "html",
169+
"content" => $message
170+
],
171+
"toRecipients" => $toArray,
172+
"ccRecipients" => $ccArray,
173+
"bccRecipients" => $bccArray
174+
];
175+
}
176+
177+
protected static function emailPrepareReply($message, $to, $cc = null, $bcc = null)
178+
{
179+
180+
$parts = explode(',', $to);
181+
$toArray = [];
182+
foreach($parts as $to) {
183+
$toArray[]["emailAddress"] = ["address" => $to];
184+
}
185+
186+
$ccArray = [];
187+
if ($cc != null) {
188+
$parts = explode(',', $cc);
189+
foreach($parts as $cc) {
190+
$ccArray[]["emailAddress"] = ["address" => $cc];
191+
}
192+
}
193+
194+
$bccArray = [];
195+
if ($bcc != null) {
196+
$parts = explode(',', $bcc);
197+
foreach($parts as $bcc) {
198+
$bccArray[]["emailAddress"] = ["address" => $bcc];
199+
}
200+
}
201+
202+
return [
203+
"comment" => $message,
204+
"toRecipients" => $toArray,
205+
"ccRecipients" => $ccArray,
206+
"bccRecipients" => $bccArray
207+
];
208+
}
209+
}

src/MsGraph.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* msgraph api documenation can be found at https://developer.msgraph.com/reference
77
**/
88

9+
use DaveismynameLaravel\MsGraph\Facades\MsGraph as Api;
910
use DaveismynameLaravel\MsGraph\Api\Contacts;
11+
use DaveismynameLaravel\MsGraph\Api\Emails;
1012
use DaveismynameLaravel\MsGraph\Models\MsGraphToken;
1113

1214
use League\OAuth2\Client\Provider\GenericProvider;
@@ -17,6 +19,7 @@
1719
class MsGraph
1820
{
1921
use Contacts;
22+
use Emails;
2023

2124
protected static $baseUrl = 'https://graph.microsoft.com/beta/';
2225

@@ -71,15 +74,16 @@ public function connect()
7174
return redirect(config('msgraph.msgraphLandingUri'));
7275

7376
} catch (IdentityProviderException $e) {
74-
die(trans('microsoftapps::module.oauth.identityException').":" . $e->getMessage());
77+
die('error:'.$e->getMessage());
7578
}
7679

7780
}
7881
}
7982

80-
public function getAccessToken()
83+
public function getAccessToken($id = null)
8184
{
82-
$token = MsGraphToken::where('user_id', auth()->id())->first();
85+
$id = ($id) ? $id : auth()->id();
86+
$token = MsGraphToken::where('user_id', $id)->first();
8387

8488
// Check if tokens exist otherwise run the oauth request
8589
if (!isset($token->access_token)) {
@@ -94,13 +98,13 @@ public function getAccessToken()
9498

9599
// Initialize the OAuth client
96100
$oauthClient = new GenericProvider([
97-
'clientId' => config('microsoftapps.clientId'),
98-
'clientSecret' => config('microsoftapps.clientSecret'),
99-
'redirectUri' => config('microsoftapps.redirectUri'),
100-
'urlAuthorize' => config('microsoftapps.urlAuthorize'),
101-
'urlAccessToken' => config('microsoftapps.urlAccessToken'),
102-
'urlResourceOwnerDetails' => config('microsoftapps.urlResourceOwnerDetails'),
103-
'scopes' => config('microsoftapps.scopes')
101+
'clientId' => config('msgraph.clientId'),
102+
'clientSecret' => config('msgraph.clientSecret'),
103+
'redirectUri' => config('msgraph.redirectUri'),
104+
'urlAuthorize' => config('msgraph.urlAuthorize'),
105+
'urlAccessToken' => config('msgraph.urlAccessToken'),
106+
'urlResourceOwnerDetails' => config('msgraph.urlResourceOwnerDetails'),
107+
'scopes' => config('msgraph.scopes')
104108
]);
105109

106110
$newToken = $oauthClient->getAccessToken('refresh_token', ['refresh_token' => $token->refresh_token]);
@@ -119,7 +123,7 @@ public function getAccessToken()
119123
protected function storeToken($access_token, $refresh_token, $expires)
120124
{
121125
//cretate a new record or if the user id exists update record
122-
MsGraphToken::updateOrCreate(['user_id' => auth()->id()], [
126+
return MsGraphToken::updateOrCreate(['user_id' => auth()->id()], [
123127
'user_id' => auth()->id(),
124128
'access_token' => $access_token,
125129
'expires' => $expires,

src/MsGraphServiceProvider.php

+1-22
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ class MsGraphServiceProvider extends ServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'daveismynamelaravel');
17-
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'daveismynamelaravel');
1816
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
19-
$this->loadRoutesFrom(__DIR__.'/routes.php');
2017

2118
// Publishing is only necessary when using the CLI.
2219
if ($this->app->runningInConsole()) {
@@ -30,25 +27,7 @@ public function boot()
3027

3128
$this->publishes([
3229
__DIR__.'/../database/migrations/create_ms_graph_tokens_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_ms_graph_tokens_tables.php",
33-
], 'migrations');
34-
35-
// Publishing the views.
36-
/*$this->publishes([
37-
__DIR__.'/../resources/views' => base_path('resources/views/vendor/daveismynamelaravel'),
38-
], 'msgraph.views');*/
39-
40-
// Publishing assets.
41-
/*$this->publishes([
42-
__DIR__.'/../resources/assets' => public_path('vendor/daveismynamelaravel'),
43-
], 'msgraph.views');*/
44-
45-
// Publishing the translation files.
46-
/*$this->publishes([
47-
__DIR__.'/../resources/lang' => resource_path('lang/vendor/daveismynamelaravel'),
48-
], 'msgraph.views');*/
49-
50-
// Registering package commands.
51-
// $this->commands([]);
30+
], 'migrations');
5231
}
5332
}
5433

src/database/migrations/2018_07_09_170632_create_ms_graph_tokens_table.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
Schema::create('ms_graph_tokens', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->integer('user_id');
19+
$table->string('email')->nullable();
1920
$table->text('access_token');
2021
$table->text('refresh_token');
2122
$table->string('expires');

0 commit comments

Comments
 (0)