1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Services\Message; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Dacastro4\LaravelGmail\GmailConnection; |
7
|
|
|
use Dacastro4\LaravelGmail\Traits\HasDecodableBody; |
8
|
|
|
use Dacastro4\LaravelGmail\Traits\HasHeaders; |
9
|
|
|
use Dacastro4\LaravelGmail\Traits\Modifiable; |
10
|
|
|
use Dacastro4\LaravelGmail\Traits\Replyable; |
11
|
|
|
use Google_Service_Gmail; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SingleMessage |
16
|
|
|
* @package Dacastro4\LaravelGmail\services |
17
|
|
|
*/ |
18
|
|
|
class Mail extends GmailConnection |
19
|
|
|
{ |
20
|
|
|
use HasDecodableBody, |
21
|
|
|
HasHeaders, |
22
|
|
|
Modifiable, |
23
|
|
|
Replyable { |
24
|
|
|
Replyable::__construct as private __rConstruct; |
25
|
|
|
Modifiable::__construct as private __mConstruct; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
public $id; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
public $internalDate; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var |
40
|
|
|
*/ |
41
|
|
|
public $labels; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var |
45
|
|
|
*/ |
46
|
|
|
public $size; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var |
50
|
|
|
*/ |
51
|
|
|
public $threatId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Google_Service_Gmail_MessagePart |
55
|
|
|
*/ |
56
|
|
|
public $payload; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Google_Service_Gmail |
60
|
|
|
*/ |
61
|
|
|
public $service; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* SingleMessage constructor. |
65
|
|
|
* |
66
|
|
|
* @param \Google_Service_Gmail_Message $message |
67
|
|
|
* @param bool $preload |
68
|
|
|
*/ |
69
|
|
|
public function __construct( \Google_Service_Gmail_Message $message, $preload = false ) |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
$this->service = new Google_Service_Gmail( $this ); |
73
|
|
|
|
74
|
|
|
$this->__rConstruct(); |
75
|
|
|
$this->__mConstruct(); |
76
|
|
|
parent::__construct(); |
77
|
|
|
|
78
|
|
|
if ( $preload ) { |
79
|
|
|
$message = $this->service->users_messages->get( 'me', $message->getId() ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->id = $message->getId(); |
83
|
|
|
$this->internalDate = $message->getInternalDate(); |
84
|
|
|
$this->labels = $message->getLabelIds(); |
85
|
|
|
$this->size = $message->getSizeEstimate(); |
86
|
|
|
$this->threatId = $message->getThreadId(); |
87
|
|
|
$this->payload = $message->getPayload(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns ID of the email |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getId() |
96
|
|
|
{ |
97
|
|
|
return $this->id; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return a UNIX version of the date |
102
|
|
|
* |
103
|
|
|
* @return int UNIX date |
104
|
|
|
*/ |
105
|
|
|
public function getInternalDate() |
106
|
|
|
{ |
107
|
|
|
return $this->internalDate; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the labels of the email |
112
|
|
|
* Example: INBOX, STARRED, UNREAD |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function getLabels() |
117
|
|
|
{ |
118
|
|
|
return $this->labels; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns approximate size of the email |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function getSize() |
127
|
|
|
{ |
128
|
|
|
return $this->size; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns threat ID of the email |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getThreatId() |
137
|
|
|
{ |
138
|
|
|
return $this->threatId; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns all the headers of the email |
143
|
|
|
* |
144
|
|
|
* @return \Google_Service_Gmail_MessagePartHeader |
145
|
|
|
*/ |
146
|
|
|
public function getHeaders() |
147
|
|
|
{ |
148
|
|
|
return $this->payload->getHeaders(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the subject of the email |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getSubject() |
157
|
|
|
{ |
158
|
|
|
return $this->getHeader( 'Subject' ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getFrom() |
162
|
|
|
{ |
163
|
|
|
|
164
|
|
|
//TODO: Fix to work with multiple emails |
165
|
|
|
$email = $this->getHeader( 'From' ); |
166
|
|
|
|
167
|
|
|
preg_match( '/<(.*)>/', $email, $matches ); |
168
|
|
|
|
169
|
|
|
return isset( $matches[ 1 ] ) ? $matches[ 1 ] : null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the original date that the email was sent |
174
|
|
|
* |
175
|
|
|
* @return Carbon |
176
|
|
|
*/ |
177
|
|
|
public function getDate() |
178
|
|
|
{ |
179
|
|
|
return Carbon::parse( $this->getHeader( 'Date' ) ); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns email of the original recipient |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getDeliveredTo() |
188
|
|
|
{ |
189
|
|
|
return $this->getHeader( 'Delivered-To' ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param bool $raw |
194
|
|
|
* |
195
|
|
|
* @return bool|string |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function getPlainTextBody( $raw = false ) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$part = $this->getBodyPart(); |
200
|
|
|
$body = $part->getBody(); |
201
|
|
|
$content = $body->getData(); |
202
|
|
|
|
203
|
|
|
return $raw ? $content : $this->getDecodedBody( $content ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string base64 version of the body |
208
|
|
|
*/ |
209
|
|
|
public function getRawPlainTextBody() |
210
|
|
|
{ |
211
|
|
|
return $this->getPlainTextBody( true ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param bool $raw |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
View Code Duplication |
public function getHtmlBody( $raw = false ) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$part = $this->getBodyPart( 'text/html' ); |
222
|
|
|
$body = $part->getBody(); |
223
|
|
|
$content = $body->getData(); |
224
|
|
|
|
225
|
|
|
return $raw ? $content : $this->getDecodedBody( $content ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string base64 version of the body |
230
|
|
|
*/ |
231
|
|
|
public function getRawHtmlBody() |
232
|
|
|
{ |
233
|
|
|
return $this->getHtmlBody( true ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function getAttachments() |
237
|
|
|
{ |
238
|
|
|
$attachments = new Collection( [] ); |
239
|
|
|
$parts = $this->payload->getParts(); |
240
|
|
|
|
241
|
|
|
/** @var \Google_Service_Gmail_MessagePart $part */ |
242
|
|
|
foreach ( $parts as $part ) { |
243
|
|
|
|
244
|
|
|
$body = $part->getBody(); |
245
|
|
|
|
246
|
|
|
if ( $body->getAttachmentId() ) { |
247
|
|
|
$attachment = ( new Attachment( $this->getId(), $part ) ); |
248
|
|
|
$attachments->push( |
249
|
|
|
$attachment |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $attachments; |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
|
|
public function hasAttachments() |
263
|
|
|
{ |
264
|
|
|
$attachments = 0; |
265
|
|
|
$parts = $this->payload->getParts(); |
266
|
|
|
|
267
|
|
|
/** @var \Google_Service_Gmail_MessagePart $part */ |
268
|
|
|
foreach ( $parts as $part ) { |
269
|
|
|
$body = $part->getBody(); |
270
|
|
|
if ( $body->getAttachmentId() ) { |
271
|
|
|
$attachments ++; |
272
|
|
|
break; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return ! ! $attachments; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $type |
281
|
|
|
* |
282
|
|
|
* @return \Google_Service_Gmail_MessagePart|null |
283
|
|
|
*/ |
284
|
|
|
private function getBodyPart( $type = 'text/html' ) |
285
|
|
|
{ |
286
|
|
|
$body = $this->payload->getParts(); |
287
|
|
|
|
288
|
|
|
if ( isset( $body[ 0 ] ) ) { |
289
|
|
|
|
290
|
|
|
$parts = $body[ 0 ]->getParts(); |
291
|
|
|
|
292
|
|
|
/** @var \Google_Service_Gmail_MessagePart $part */ |
293
|
|
|
foreach ( $parts as $part ) { |
294
|
|
|
if ( $part->getMimeType() === $type ) { |
295
|
|
|
break; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $part ?: null; |
|
|
|
|
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return null; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get's the gmail information from the Mail |
308
|
|
|
* |
309
|
|
|
* @return Mail |
310
|
|
|
*/ |
311
|
|
|
public function load() |
312
|
|
|
{ |
313
|
|
|
$message = $this->service->users_messages->get( 'me', $this->getId() ); |
314
|
|
|
|
315
|
|
|
return new self( $message ); |
316
|
|
|
} |
317
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.