diff options
| author | Horus3 | 2016-07-03 18:07:29 +0200 |
|---|---|---|
| committer | Horus3 | 2016-07-03 18:07:29 +0200 |
| commit | f5cf66b6929928fe5a4a6cf227f9a2f2195329e5 (patch) | |
| tree | 94155faa0b84a236503ee0843eec749989fd9625 /imap.php | |
| download | imap2rss-f5cf66b6929928fe5a4a6cf227f9a2f2195329e5.tar.gz | |
Initial commit.
Diffstat (limited to 'imap.php')
| -rw-r--r-- | imap.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/imap.php b/imap.php new file mode 100644 index 0000000..191447c --- /dev/null +++ b/imap.php @@ -0,0 +1,69 @@ +<?php +function date_compare($a, $b) +{ + $t1 = strtotime($a['date']); + $t2 = strtotime($b['date']); + return -($t1 - $t2); +} + +function getMail($user, $pass, $inbox, $max_items){ + $mail = array(); + + foreach($inbox as $cur_inbox) { + try { + $cur_mail = getMailbox($user, $pass, $max_items, $cur_inbox); + if ( false === $cur_mail ) { + // Mailbox is empty. + continue; + } + } catch( Exception $e ) { + header( $_SERVER["SERVER_PROTOCOL"] . " 403 Forbidden" ); + exit(1); + } + + $mail = array_merge($mail, $cur_mail); + + } + + usort($mail, 'date_compare'); + + return $mail; +} + +function getMailbox( $user, $password, $max_items, $inbox, $imap_host = "mx.iamfabulous.de" ) { + require_once __DIR__ . '/vendor/autoload.php'; + + $mailbox = new PhpImap\Mailbox('{'.$imap_host.'}'.$inbox, $user, $password); + + // Read all messaged into an array: + $mailsIds = $mailbox->searchMailbox('ALL'); + if(!$mailsIds) { + #throw new Exception('Mailbox is empty'); + return false; + } + + $return = array(); + + if ( (count($mailsIds) - $max_items) < 0) { + $limit = 0; + } else { + $limit = count($mailsIds) - $max_items; + } + + $cnt = 0; + for( $i = count($mailsIds) - 1; $i >= $limit; $i-- ) { + $mbox = $mailbox->getMail($mailsIds[$i], $markAsSeen = false); + $return[$cnt]["date"] = $mbox->date; + $return[$cnt]["subject"] = $mbox->subject; + $return[$cnt]["fromName"] = $mbox->fromName; + $return[$cnt]["fromAddress"] = $mbox->fromAddress; + $return[$cnt]["to"] = $mbox->to; + $return[$cnt]["toString"] = $mbox->toString; + $return[$cnt]["messageId"] = $mbox->messageId; + $return[$cnt]["textPlain"] = $mbox->textPlain; + $return[$cnt]["textHtml"] = $mbox->textHtml; + $return[$cnt]["mailbox"] = $inbox; + $cnt++; + } + return $return; +} |
