You are here

public function MailhandlerPhpImapRetrieve::retrieve in Mailhandler 7.2

Same name and namespace in other branches
  1. 6.2 modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php \MailhandlerPhpImapRetrieve::retrieve()

Connect to mailbox and run message retrieval.

Parameters

object $mailbox: The mailbox to retrieve from.

string|null $filter_name: (optional) Mailhandler filter to restrict what messages are retrieved.

Return value

array Retrieved messages.

Overrides MailhandlerRetrieve::retrieve

File

modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php, line 24
Definition of MailhandlerPhpImapRetrieve class.

Class

MailhandlerPhpImapRetrieve
Retrieve messages using PHP IMAP library.

Code

public function retrieve($mailbox, $filter_name = 'MailhandlerFilters') {
  extract($mailbox->settings);
  if (!($result = $this
    ->open_mailbox($mailbox))) {
    mailhandler_report('error', 'Unable to connect to %mail. Please check the <a href="@mailbox-edit">connection settings</a> for this mailbox.', array(
      '%mail' => $mailbox->mail,
      '@mailbox-edit' => url(MAILHANDLER_MENU_PREFIX . '/mailhandler/list/' . $mailbox->mail . '/edit'),
    ));
    $this
      ->report_errors($mailbox);
  }
  $new = $this
    ->get_unread_messages($result, $mailbox);
  $messages = array();
  $retrieved = 0;
  while ($new && (!$limit || $retrieved < $limit)) {
    if (!($message = $this
      ->retrieve_message($result, $mailbox, array_shift($new), $filter_name))) {
      continue;
    }
    $messages[] = $message;
    $retrieved++;
  }
  mailhandler_report('status', 'Mailbox %mail was checked and contained %retrieved messages.', array(
    '%mail' => $mailbox->admin_title,
    '%retrieved' => $retrieved,
  ));
  $this
    ->close_mailbox($result, $mailbox);
  return $messages;
}