-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathFetchEmails.php
672 lines (586 loc) · 24.3 KB
/
FetchEmails.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<?php
namespace App\Console\Commands;
use App\Attachment;
use App\Conversation;
use App\Customer;
use App\Email;
use App\Events\ConversationCustomerChanged;
use App\Events\CustomerCreatedConversation;
use App\Events\CustomerReplied;
use App\Events\UserReplied;
use App\Misc\Mail;
use App\Mailbox;
use App\Option;
use App\Subscription;
use App\Thread;
use App\User;
use Illuminate\Console\Command;
use Webklex\IMAP\Client;
class FetchEmails extends Command
{
/**
* Period in days for fetching emails from mailbox email.
*/
const CHECK_PERIOD = 3;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'freescout:fetch-emails';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch emails from mailboxes addresses';
/**
* Current mailbox.
*
* @var Mailbox
*/
public $mailbox;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$now = time();
$successfully = true;
Option::set('fetch_emails_last_run', $now);
// Get active mailboxes
$mailboxes = Mailbox::where('in_protocol', '<>', '')
->where('in_server', '<>', '')
->where('in_port', '<>', '')
->where('in_username', '<>', '')
->where('in_password', '<>', '')
->get();
foreach ($mailboxes as $mailbox) {
$this->info('['.date('Y-m-d H:i:s').'] Mailbox: '.$mailbox->name);
$this->mailbox = $mailbox;
try {
$this->fetch($mailbox);
} catch (\Exception $e) {
$successfully = false;
$this->logError('Error: '.$e->getMessage().'; File: '.$e->getFile().' ('.$e->getLine().')').')';
}
}
if ($successfully) {
Option::set('fetch_emails_last_successful_run', $now);
}
// Middleware Terminate handler is not launched for commands,
// so we need to run processing subscription events manually
Subscription::processEvents();
}
public function fetch($mailbox)
{
$client = new Client([
'host' => $mailbox->in_server,
'port' => $mailbox->in_port,
'encryption' => $mailbox->getInEncryptionName(),
'validate_cert' => true,
'username' => $mailbox->in_username,
'password' => $mailbox->in_password,
'protocol' => $mailbox->getInProtocolName(),
]);
// Connect to the Server
$client->connect();
// Get folder
$folder = $client->getFolder('INBOX');
if (!$folder) {
throw new \Exception('Could not get mailbox folder: INBOX', 1);
}
$folders = [$folder];
// It would be good to be able to fetch emails from Spam folder into Spam folder of the mailbox
// But not all mail servers provide access to it.
// For example DreamHost does have a Spam folder but allows IMAP access to the following folders only:
// ./cur
// ./new
// ./tmp
// $folders = [];
// if ($mailbox->in_protocol == Mailbox::IN_PROTOCOL_IMAP) {
// try {
// //$folders = $client->getFolders();
// } catch (\Exception $e) {
// // Do nothing
// }
// }
// if (!count($folders)) {
// $folders = [$client->getFolder('INBOX')];
// }
foreach ($folders as $folder) {
$this->line('['.date('Y-m-d H:i:s').'] Folder: '.$folder->name);
// Get unseen messages for a period
$messages = $folder->query()->unseen()->since(now()->subDays(self::CHECK_PERIOD))->leaveUnread()->get();
if ($client->getLastError()) {
// Throw exception for INBOX only
if ($folder->name == 'INBOX') {
throw new Exception($client->getLastError(), 1);
} else {
$this->error('['.date('Y-m-d H:i:s').'] '.$client->getLastError());
}
}
$this->line('['.date('Y-m-d H:i:s').'] Fetched: '.count($messages));
$message_index = 1;
// We have to sort messages manually, as they can be in non-chronological order
$messages = $this->sortMessage($messages);
foreach ($messages as $message_id => $message) {
try {
$this->line('['.date('Y-m-d H:i:s').'] '.$message_index.') '.$message->getSubject());
$message_index++;
// Check if message already fetched
if (Thread::where('message_id', $message_id)->first()) {
$this->line('['.date('Y-m-d H:i:s').'] Message with such Message-ID has been fetched before: '.$message_id);
$message->setFlag(['Seen']);
continue;
}
// From
$from = $message->getReplyTo();
if (!$from) {
$from = $message->getFrom();
}
if (!$from) {
$this->logError('From is empty');
$message->setFlag(['Seen']);
continue;
} else {
$from = $this->formatEmailList($from);
$from = $from[0];
}
// Detect prev thread
$is_reply = false;
$prev_thread = null;
$user_id = null;
$user = null; // for user reply only
$message_from_customer = true;
$in_reply_to = $message->getInReplyTo();
$references = $message->getReferences();
$attachments = $message->getAttachments();
// Is it a bounce message
$is_bounce = false;
$bounce_attachment = null;
// Determine bounce by attachment
if ($message->hasAttachments()) {
foreach ($attachments as $attachment) {
if (!empty(Attachment::$types[$attachment->getType()]) && Attachment::$types[$attachment->getType()] == Attachment::TYPE_MESSAGE) {
if (in_array($attachment->getName(), ['RFC822', 'DELIVERY-STATUS'])) {
$is_bounce = true;
$bounce_attachment = $attachment;
break;
}
}
}
}
// Is it a message from Customer or User replied to the notification
preg_match('/^'.\App\Misc\Mail::MESSAGE_ID_PREFIX_NOTIFICATION."\-(\d+)\-(\d+)\-/", $in_reply_to, $m);
if (!$is_bounce && !empty($m[1]) && !empty($m[2])) {
// Reply from User to the notification
$prev_thread = Thread::find($m[1]);
$user_id = $m[2];
$user = User::find($user_id);
$message_from_customer = false;
$is_reply = true;
if (!$user) {
$this->logError('User not found: '.$user_id);
$message->setFlag(['Seen']);
continue;
}
$this->line('['.date('Y-m-d H:i:s').'] Message from: User');
} elseif (!$is_bounce && ($user = User::where('email', $from)->first()) && $in_reply_to && ($prev_thread = Thread::where('message_id', $in_reply_to)->first()) && $prev_thread->created_by_user_id == $user->id) {
// Reply from customer to his reply to the notification
$user_id = $user->id;
$message_from_customer = false;
$is_reply = true;
} else {
// Message from Customer
$this->line('['.date('Y-m-d H:i:s').'] Message from: Customer');
$prev_message_id = '';
if (!$is_bounce) {
if ($in_reply_to) {
$prev_message_id = $in_reply_to;
} elseif ($references) {
if (!is_array($references)) {
$references = array_filter(preg_split('/[, <>]/', $references));
}
// Maybe we need to check all references
$prev_message_id = $references[0];
}
if ($prev_message_id) {
$prev_thread_id = '';
// Customer replied to the email from user
preg_match('/^'.\App\Misc\Mail::MESSAGE_ID_PREFIX_REPLY_TO_CUSTOMER."\-(\d+)\-/", $prev_message_id, $m);
if (!empty($m[1])) {
$prev_thread_id = $m[1];
}
// Customer replied to the auto reply
if (!$prev_thread_id) {
preg_match('/^'.\App\Misc\Mail::MESSAGE_ID_PREFIX_AUTO_REPLY."\-(\d+)\-/", $prev_message_id, $m);
if (!empty($m[1])) {
$prev_thread_id = $m[1];
}
}
if ($prev_thread_id) {
$prev_thread = Thread::find($prev_thread_id);
} else {
// Customer replied to his own message
$prev_thread = Thread::where('message_id', $prev_message_id)->first();
}
}
if (!empty($prev_thread)) {
$is_reply = true;
}
}
}
if ($message->hasHTMLBody()) {
// Get body and replace :cid with images URLs
$body = $message->getHTMLBody(true);
$body = $this->separateReply($body, true, $is_reply);
} else {
$body = $message->getTextBody();
$body = $this->separateReply($body, false, $is_reply);
}
if (!$body) {
$this->logError('Message body is empty');
$message->setFlag(['Seen']);
continue;
}
$subject = $message->getSubject();
$to = $this->formatEmailList($message->getTo());
//$to = $mailbox->removeMailboxEmailsFromList($to);
$cc = $this->formatEmailList($message->getCc());
//$cc = $mailbox->removeMailboxEmailsFromList($cc);
$bcc = $this->formatEmailList($message->getBcc());
//$bcc = $mailbox->removeMailboxEmailsFromList($bcc);
// Create customers
$emails = array_merge($message->getFrom(), $message->getReplyTo(), $message->getTo(), $message->getCc(), $message->getBcc());
$this->createCustomers($emails, $mailbox->getEmails());
if ($message_from_customer) {
$new_thread_id = $this->saveCustomerThread($mailbox->id, $message_id, $prev_thread, $from, $to, $cc, $bcc, $subject, $body, $attachments, $message->getHeader());
} else {
// Check if From is the same as user's email.
// If not we send an email with information to the sender.
if (Email::sanitizeEmail($user->email) != Email::sanitizeEmail($from)) {
$this->logError("From address {$from} is not the same as user {$user->id} email: ".$user->email);
$message->setFlag(['Seen']);
// todo: send email with information
// Unable to process your update
// Your email update couldn't be processed
// If you are trying to update a conversation, remember you must respond from the same email address that's on your account. To send your update, please try again and send from your account email address (the email you login with).
continue;
}
$new_thread_id = $this->saveUserThread($mailbox, $message_id, $prev_thread, $user_id, $from, $to, $cc, $bcc, $body, $attachments, $message->getHeader());
}
if ($new_thread_id) {
$message->setFlag(['Seen']);
$this->line('['.date('Y-m-d H:i:s').'] Thread successfully created: '.$new_thread_id);
} else {
$this->logError('Error occured processing message');
}
} catch (\Exception $e) {
$message->setFlag(['Seen']);
$this->logError('Error: '.$e->getMessage().'; File: '.$e->getFile().' ('.$e->getLine().')').')';
}
}
}
$client->disconnect();
}
public function logError($message)
{
$this->error('['.date('Y-m-d H:i:s').'] '.$message);
$mailbox_name = '';
if ($this->mailbox) {
$mailbox_name = $this->mailbox->name;
}
try {
activity()
->withProperties([
'error' => $message,
'mailbox' => $mailbox_name,
])
->useLog(\App\ActivityLog::NAME_EMAILS_FETCHING)
->log(\App\ActivityLog::DESCRIPTION_EMAILS_FETCHING_ERROR);
} catch (\Exception $e) {
// Do nothing
}
}
/**
* Save email from customer as thread.
*/
public function saveCustomerThread($mailbox_id, $message_id, $prev_thread, $from, $to, $cc, $bcc, $subject, $body, $attachments, $headers)
{
// Find conversation
$new = false;
$conversation = null;
$now = date('Y-m-d H:i:s');
$customer = Customer::create($from);
if ($prev_thread) {
$conversation = $prev_thread->conversation;
// If reply came from another customer: change customer, add original as CC
if ($conversation->customer_id != $customer->id) {
$prev_customer_id = $conversation->customer_id;
$prev_customer_email = $conversation->customer_email;
$cc[] = $conversation->customer_email;
$conversation->customer_id = $customer->id;
}
} else {
// Create conversation
$new = true;
$conversation = new Conversation();
$conversation->type = Conversation::TYPE_EMAIL;
$conversation->state = Conversation::STATE_PUBLISHED;
$conversation->subject = $subject;
$conversation->setPreview($body);
if (count($attachments)) {
$conversation->has_attachments = true;
}
$conversation->mailbox_id = $mailbox_id;
$conversation->customer_id = $customer->id;
$conversation->created_by_customer_id = $customer->id;
$conversation->source_via = Conversation::PERSON_CUSTOMER;
$conversation->source_type = Conversation::SOURCE_TYPE_EMAIL;
}
// Save extra recipients to CC
$conversation->setCc(array_merge($cc, $to));
$conversation->setBcc($bcc);
$conversation->customer_email = $from;
// Reply from customer makes conversation active
$conversation->status = Conversation::STATUS_ACTIVE;
$conversation->last_reply_at = $now;
$conversation->last_reply_from = Conversation::PERSON_CUSTOMER;
// Set folder id
$conversation->updateFolder();
$conversation->save();
// Thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_CUSTOMER;
$thread->status = $conversation->status;
$thread->state = Thread::STATE_PUBLISHED;
$thread->message_id = $message_id;
$thread->headers = $headers;
$thread->body = $body;
$thread->from = $from;
$thread->setTo($to);
$thread->setCc($cc);
$thread->setBcc($bcc);
$thread->source_via = Thread::PERSON_CUSTOMER;
$thread->source_type = Thread::SOURCE_TYPE_EMAIL;
$thread->customer_id = $customer->id;
$thread->created_by_customer_id = $customer->id;
if ($new) {
$thread->first = true;
}
$thread->save();
$has_attachments = $this->saveAttachments($attachments, $thread->id);
if ($has_attachments) {
$thread->has_attachments = true;
$thread->save();
}
if ($new) {
event(new CustomerCreatedConversation($conversation, $thread));
} else {
event(new CustomerReplied($conversation, $thread));
}
// Conversation customer changed
if ($prev_customer_id) {
event(new ConversationCustomerChanged($conversation, $prev_customer_id, $prev_customer_email, null, $customer));
}
return $thread->id;
}
/**
* Save email reply from user as thread.
*/
public function saveUserThread($mailbox, $message_id, $prev_thread, $user_id, $from, $to, $cc, $bcc, $body, $attachments, $headers)
{
$conversation = null;
$now = date('Y-m-d H:i:s');
$conversation = $prev_thread->conversation;
// Determine assignee
// maybe we need to check mailbox->ticket_assignee here, maybe not
if (!$conversation->user_id) {
$conversation->user_id = $user_id;
}
// Save extra recipients to CC
$conversation->setCc(array_merge($cc, $to));
$conversation->setBcc($bcc);
// Reply from user makes conversation pending
$conversation->status = Conversation::STATUS_PENDING;
$conversation->last_reply_at = $now;
$conversation->last_reply_from = Conversation::PERSON_USER;
$conversation->user_updated_at = $now;
// Set folder id
$conversation->updateFolder();
$conversation->save();
// Thread
$thread = new Thread();
$thread->conversation_id = $conversation->id;
$thread->user_id = $conversation->user_id;
$thread->type = Thread::TYPE_MESSAGE;
$thread->status = $conversation->status;
$thread->state = Thread::STATE_PUBLISHED;
$thread->message_id = $message_id;
$thread->headers = $headers;
$thread->body = $body;
$thread->from = $from;
// To must be customer's email
$thread->setTo([$conversation->customer_email]);
$thread->setCc($cc);
$thread->setBcc($bcc);
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_EMAIL;
$thread->customer_id = $conversation->customer_id;
$thread->created_by_user_id = $user_id;
$thread->save();
$has_attachments = $this->saveAttachments($attachments, $thread->id);
if ($has_attachments) {
$thread->has_attachments = true;
$thread->save();
}
event(new UserReplied($conversation, $thread));
return $thread->id;
}
/**
* Save attachments from email.
*
* @param array $attachments
* @param int $thread_id
*
* @return bool
*/
public function saveAttachments($email_attachments, $thread_id)
{
$has_attachments = false;
foreach ($email_attachments as $email_attachment) {
$create_result = Attachment::create(
$email_attachment->getName(),
$email_attachment->getMimeType(),
Attachment::typeNameToInt($email_attachment->getType()),
$email_attachment->getContent(),
'',
false,
$thread_id
);
if ($create_result) {
$has_attachments = true;
}
}
return $has_attachments;
}
/**
* Separate reply in the body.
*
* @param string $body
*
* @return string
*/
public function separateReply($body, $is_html, $is_reply)
{
$cmp_reply_length_desc = function ($a, $b) {
if (mb_strlen($a) == mb_strlen($b)) {
return 0;
}
return (mb_strlen($a) < mb_strlen($b)) ? -1 : 1;
};
if ($is_html) {
// Extract body content from HTML
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8'));
libxml_use_internal_errors(false);
$bodies = $dom->getElementsByTagName('body');
if ($bodies->length == 1) {
$body_el = $bodies->item(0);
$body = $dom->saveHTML($body_el);
}
preg_match("/<body[^>]*>(.*?)<\/body>/is", $body, $matches);
if (count($matches)) {
$body = $matches[1];
}
} else {
$body = nl2br($body);
}
// This is reply, we need to separate reply text from old text
if ($is_reply) {
// Check all separators and choose the shortest reply
$reply_bodies = [];
foreach (Mail::$alternative_reply_separators as $alt_separator) {
$parts = explode($alt_separator, $body);
if (count($parts) > 1) {
$reply_bodies[] = $parts[0];
}
}
if (count($reply_bodies)) {
usort($reply_bodies, $cmp_reply_length_desc);
return $reply_bodies[0];
}
}
return $body;
}
/**
* Conver email object to plain emails.
*
* @param array $obj_list
*
* @return array
*/
public function formatEmailList($obj_list)
{
$plain_list = [];
foreach ($obj_list as $item) {
$item->mail = Email::sanitizeEmail($item->mail);
if ($item->mail) {
$plain_list[] = $item->mail;
}
}
return $plain_list;
}
/**
* We have to sort messages manually, as they can be in non-chronological order.
*
* @param Collection $messages
*
* @return Collection
*/
public function sortMessage($messages)
{
$messages = $messages->sortBy(function ($message, $key) {
return $message->getDate()->timestamp;
});
return $messages;
}
/**
* Create customers from emails.
*
* @param array $emails_data
*/
public function createCustomers($emails, $exclude_emails)
{
foreach ($emails as $item) {
// Email belongs to mailbox
if (in_array(Email::sanitizeEmail($item->mail), $exclude_emails)) {
continue;
}
$data = [];
if (!empty($item->personal)) {
$name_parts = explode(' ', $item->personal, 2);
$data['first_name'] = $name_parts[0];
if (!empty($name_parts[1])) {
$data['last_name'] = $name_parts[1];
}
}
Customer::create($item->mail, $data);
}
}
}