You are here

function mailhandler_retrieve_message in Mailhandler 7

Same name and namespace in other branches
  1. 6 mailhandler.retrieve.inc \mailhandler_retrieve_message()

Retrieve individual messages from an IMAP result

Parameters

$result: IMAP stream

$mailbox: Array of mailbox configuration

$i: Int message number

$context: Array used by batch API

Return value

unknown_type

1 call to mailhandler_retrieve_message()
mailhandler_retrieve in ./mailhandler.retrieve.inc
Connect to mailbox and run message retrieval
1 string reference to 'mailhandler_retrieve_message'
mailhandler_retrieve in ./mailhandler.retrieve.inc
Connect to mailbox and run message retrieval

File

./mailhandler.retrieve.inc, line 363

Code

function mailhandler_retrieve_message($result, $mailbox, $i, &$context) {

  // Required for batch API.
  if (!$result) {
    $result = mailhandler_open_mailbox($mailbox);
  }
  $header = imap_header($result, imap_msgno($result, $i));

  // Initialize the subject in case it's missing.
  if (!isset($header->subject)) {
    $header->subject = '';
  }
  $mime = explode(',', $mailbox['mime']);

  // Get the first text part - this will be the node body
  $origbody = mailhandler_get_part($result, $i, $mime[0]);

  // If we didn't get a body from our first attempt, try the alternate format (HTML or PLAIN)
  if (!$origbody) {
    $origbody = mailhandler_get_part($result, $i, $mime[1]);
  }

  // Parse MIME parts, so all mailhandler modules have access to
  // the full array of mime parts without having to process the email.
  $mimeparts = mailhandler_get_parts($result, $i);

  // Is this an empty message with no body and no mimeparts?
  if (!$origbody && !$mimeparts) {

    // @TODO: Log that we got an empty email?
    // TODO: We should not just close here, need to keep open in case of cron/auto
    imap_close($result);
    return;
  }

  // Don't delete while we're only getting new messages
  if ($mailbox['delete_after_read']) {
    imap_delete($result, $i, FT_UID);
  }
  $message = array(
    'header' => $header,
    'origbody' => $origbody,
    'mimeparts' => $mimeparts,
    'mailbox' => $mailbox,
  );

  // If using batch API, must close imap stream.  Cron uses single stream.
  if (!empty($context) && array_key_exists('sandbox', $context)) {
    $context['results'][] = $message;
    imap_close($result, CL_EXPUNGE);
  }
  else {
    return $message;
  }
}