This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.php
66 lines (53 loc) · 2.23 KB
/
fetch.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
<?php
// TODO Add attachments to sender message
// TODO Skip large attachments
require_once 'app/config.php';
if (!file_exists(QUEUE_FILE) || filesize(QUEUE_FILE) <= 0) {
die('Run `php app/update.php` to initialize database.');
}
foreach (CONFIGURATION as $listName => $configuration) {
$mailbox = new PhpImap\Mailbox(sprintf('{%s:993/imap/ssl}INBOX', $configuration['IMAP']['HOST']), $configuration['IMAP']['USER'], $configuration['IMAP']['PASSWORD'], MAIL_ATTACHMENTS);
$mailbox->setExpungeOnDisconnect(true);
$mailsIds = $mailbox->searchMailbox($configuration['IMAP']['SEARCH']);
$public = isset($configuration['PUBLIC']) ? $configuration['PUBLIC'] : false;
foreach ($mailsIds as $message_uid) {
$recieved = $mailbox->getMail($message_uid, false);
$external = !isset($configuration['LIST'][$recieved->fromAddress]);
if (!$public && $external) {
$mailbox->markMailAsRead($message_uid);
$mailbox->moveMail($message_uid, $configuration['IMAP']['ERRORS']);
Analog::info(sprintf('Mail[%s] not in mailing list', $recieved->fromAddress ));
continue;
}
if ($queue->has('messages', [ 'message_uid' => $recieved->messageId ])) {
$mailbox->markMailAsRead($message_uid);
$mailbox->moveMail($message_uid, $configuration['IMAP']['RECEIVED']);
Analog::info(sprintf('Mail[%s] already exists', $recieved->messageId ));
continue;
}
$messageDate = new DateTime($recieved->date);
$stmt = $queue->insert('messages', [
'list_name' => $listName,
'message_uid' => $recieved->messageId,
'message_date' => $messageDate->getTimestamp(),
'message_from' => $recieved->fromAddress,
'external' => $external,
'subject' => $recieved->subject,
'plain'=> $recieved->textPlain,
'html'=> $recieved->textHtml
]);
if ($stmt->rowCount() > 0) {
$message_id = $queue->id();
foreach ($configuration['LIST'] as $sender => $name) {
if ($name === 'IGNORE') continue;
$queue->insert('queue', [
'message_id' => $message_id,
'send_to' => $sender
]);
}
}
$mailbox->markMailAsRead($message_uid);
$mailbox->moveMail($message_uid, $configuration['IMAP']['RECEIVED']);
}
$mailbox->disconnect();
}