|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Dacastro4\LaravelGmail\LaravelGmailClass; |
|
6
|
|
|
use Dacastro4\LaravelGmail\Services\Message\Mail; |
|
7
|
|
|
use Dacastro4\LaravelGmail\Traits\SendsParameters; |
|
8
|
|
|
use Google_Service_Gmail; |
|
9
|
|
|
|
|
10
|
|
|
class Message |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
use SendsParameters; |
|
14
|
|
|
|
|
15
|
|
|
public $service; |
|
16
|
|
|
|
|
17
|
|
|
public $preload = false; |
|
18
|
|
|
|
|
19
|
|
|
public $pageToken; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Optional parameter for getting single and multiple emails |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $params = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Message constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param LaravelGmailClass $client |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( LaravelGmailClass $client ) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->service = new Google_Service_Gmail( $client ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns a collection of Mail instances |
|
40
|
|
|
* |
|
41
|
|
|
* @param null|string $pageToken |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Support\Collection |
|
44
|
|
|
*/ |
|
45
|
|
|
public function all( $pageToken = null ) |
|
46
|
|
|
{ |
|
47
|
|
|
if ( ! is_null( $pageToken ) ) { |
|
48
|
|
|
$this->add( $pageToken, 'pageToken' ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$messages = []; |
|
52
|
|
|
$response = $this->service->users_messages->listUsersMessages( 'me', $this->params ); |
|
53
|
|
|
|
|
54
|
|
|
$this->pageToken = $response->getNextPageToken(); |
|
55
|
|
|
|
|
56
|
|
|
$allMessages = $response->getMessages(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ( $allMessages as $message ) { |
|
59
|
|
|
$messages[] = new Mail( $message, $this->preload ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return collect( $messages ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns next page if available of messages or an empty collection |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Illuminate\Support\Collection |
|
69
|
|
|
*/ |
|
70
|
|
|
public function next() |
|
71
|
|
|
{ |
|
72
|
|
|
if($this->pageToken) { |
|
73
|
|
|
return $this->all($this->pageToken); |
|
74
|
|
|
} else { |
|
75
|
|
|
return collect([]); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns boolean if the page token variable is null or not |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function hasNextPage() |
|
85
|
|
|
{ |
|
86
|
|
|
return !!$this->pageToken; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Limit the messages coming from the query |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $number |
|
93
|
|
|
* |
|
94
|
|
|
* @return Message |
|
95
|
|
|
*/ |
|
96
|
|
|
public function take( $number ) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->params[ 'maxResults' ] = abs( (int) $number ); |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $id |
|
105
|
|
|
* |
|
106
|
|
|
* @return Mail |
|
107
|
|
|
*/ |
|
108
|
|
|
public function get( $id ) |
|
109
|
|
|
{ |
|
110
|
|
|
$message = $this->service->users_messages->get( 'me', $id ); |
|
111
|
|
|
|
|
112
|
|
|
return new Mail( $message ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Filter to get only unread emalis |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function unread() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->add( 'is:unread' ); |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Filter to get only unread emalis |
|
129
|
|
|
* |
|
130
|
|
|
* @param $query |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function subject( $query ) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->add( "[{$query}]" ); |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Filter to get only emails from a specific email address |
|
143
|
|
|
* |
|
144
|
|
|
* @param $email |
|
145
|
|
|
* |
|
146
|
|
|
* @return $this |
|
147
|
|
|
*/ |
|
148
|
|
|
public function from( $email ) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->add( "from:{$email}" ); |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Filter to get only emails to a specific email address |
|
157
|
|
|
* |
|
158
|
|
|
* @param $email |
|
159
|
|
|
* |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function to( $email ) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->add( "to:{$email}" ); |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* add an array of from addresses |
|
171
|
|
|
* |
|
172
|
|
|
* @param $emails |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function fromThese( $emails ) |
|
177
|
|
|
{ |
|
178
|
|
|
$emailsCount = count( $emails ); |
|
179
|
|
|
for ( $i = 0; $i < $emailsCount; $i ++ ) { |
|
180
|
|
|
! $i ? $this->add( "{from:$emails[$i]" ) : $i == $emailsCount - 1 ? $this->add( "from:$emails[$i]}" ) : $this->from( $emails[ $i ] ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Filters emails by tag |
|
188
|
|
|
* Example: |
|
189
|
|
|
* * starred |
|
190
|
|
|
* * inbox |
|
191
|
|
|
* * spam |
|
192
|
|
|
* * chats |
|
193
|
|
|
* * sent |
|
194
|
|
|
* * draft |
|
195
|
|
|
* * trash |
|
196
|
|
|
* |
|
197
|
|
|
* @param $box |
|
198
|
|
|
* |
|
199
|
|
|
* @return Message |
|
200
|
|
|
*/ |
|
201
|
|
|
public function in( $box = 'inbox' ) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->add( "in:{$box}" ); |
|
204
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Determines if the email has attachments |
|
210
|
|
|
* |
|
211
|
|
|
* @return $this |
|
212
|
|
|
*/ |
|
213
|
|
|
public function hasAttachment() |
|
214
|
|
|
{ |
|
215
|
|
|
$this->add( 'has:attachment' ); |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Preload the information on each Mail objects. |
|
222
|
|
|
* If is not preload you will have to call the load method from the Mail class |
|
223
|
|
|
* @see Mail::load() |
|
224
|
|
|
* |
|
225
|
|
|
* @return $this |
|
226
|
|
|
*/ |
|
227
|
|
|
public function preload() |
|
228
|
|
|
{ |
|
229
|
|
|
$this->preload = true; |
|
230
|
|
|
|
|
231
|
|
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|